New to Telerik Test Studio? Download free 30-day trial

Generate Random Text

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.

Without code

See our article on the Extraction feature.

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.

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<HtmlInputSearch>("sb_form_q").Text = randomText;
Find.ById<HtmlInputSubmit>("sb_form_go").Click();
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 HtmlInputSearch)("sb_form_q").Text = randomText
Find.ById(Of HtmlInputSubmit)("sb_form_go").Click()

See also: Check our blog post How to input random data in your test.

In this article