New to Telerik Test Studio? Start a 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.
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<HtmlInputSearch>("sb_form_q").Text = randomText;
Find.ById<HtmlInputSubmit>("sb_form_go").Click();See also: Check our blog post How to input random data in your test.