Generate Random Text

PROBLEM

I would like to generate random text in Test Studio. This text will be used in an input control or for a similar purpose.

SOLUTION

There are two possible approaches: with or without code.

With Code

The .NET Framework provides the System.Random class which can be used to generate random text.

 

In this example, we'll navigate to Bing.com and enter random text into the search box.

 

C#

ActiveBrowser.NavigateTo("http://www.bing.com");
 
//Define the length of the text
int length = 8;
//Define the included characters
string charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
Random random = new Random();
string randomText = new String(Enumerable.Repeat(charSet, length).Select(set => set[random.Next(set.Length)]).ToArray());
 
Find.ById<HtmlInputText>("sb_form_q").Text = randomText;
Find.ById<HtmlInputSubmit>("sb_form_go").Click();

 

Visual Basic

ActiveBrowser.NavigateTo("http://www.bing.com")
 
'Define the length of the text
Dim length As Integer = 8
'Define the included characters
Dim charSet As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
Dim random As New Random()
Dim randomText As String = New [String](Enumerable.Repeat(charSet, length).[Select](Function([set]) [set](random.[Next]([set].Length))).ToArray())
 
Find.ById(Of HtmlInputText)("sb_form_q").Text = randomText
Find.ById(Of HtmlInputSubmit)("sb_form_go").Click()

Without Code

See our article on the Extraction feature. You can also review our Telerik TV episode on Using Element Extraction for a video walk-through of this process.