I had a scenario that requires several fields to be unique, and I wanted to have a web test for this scenario. I had the problem of generating these unique parameters to be unique
The first naïve solution I used is to create these unique values using excel and then saving in csv file. Then in my scenario, I set the parameter to a data source from this csv file. But this required me every time I run the scenario to remove the values that are used
Second trial for me, I converted the recorded web test to a coded test, this allowed me to to change the parameters programmatically and also gave me lots of flexibility on the test, on the other hand I lost the usability and flexibility of the web test designer, where I can record more to these test, extract parameters easily, etc..
In the coded test, I was looking for the request that has the parameters that needs to be unique, then I created a function to create a 10 digits random number and returns this as string, like below
public string generateRandomId()
{
Random rand = new Random();
string[] random = new string[10];
for (int i = 0; i < 10; i++)
{
random[i] = rand.Next(0, 9).ToString();
}
return string.Join("",random);
}
Then at the request parameter setting, I changed from
request8Body.FormPostParameters.Add("Form1:referenceNumberText", "12341212323");
to
request8Body.FormPostParameters.Add("Form1:referenceNumberText", generateRandomId());
Third trial which is the best as believe, I created a web test plugin to set a context parameter at the beginning of the test
First I created a context parameter, by doing right click on the web test, then selecting Add Context Parameter
Then I renamed this parameter to RandomText
Then following the instructions here, http://msdn.microsoft.com/en-US/library/ms243191(v=vs.80).aspx I created a new project in my solution, and added a new class, called it WebTestPlugin.cs
Added reference to Microsoft.VisualStudio.QualityTools.WebTestFramework and added the following line at the top
using Microsoft.VisualStudio.TestTools.WebTesting;
Then Changed the class name and inherited from WebTestPlugin, the code looked like below
public class webTestPlugin : WebTestPlugin
{
string randomTxt;
public override void PreWebTest(object sender, PreWebTestEventArgs e)
{
randomTxt = generateRandomId();
e.WebTest.Context["RandomText"] = randomTxt;
base.PreWebTest(sender, e);
}
{
Random rand = new Random();
string[] random = new string[10];
for (int i = 0; i < 10; i++)
{
random[i] = rand.Next(0, 9).ToString();
}
return string.Join("",random);
}
}
Then I went to my web test project, added a reference to my plugin project
Then added the plugin to my web test
by doing the above, I had the flexibility of web test designer, and the flexibility of writing my own code in the web test
Will this work for coded ui test?
ReplyDeleteCoded UI test, is originally code, there is no need to do any conversion, and you can add your own code to it directly to generate whatever data you need, including unique IDs
Delete