Typically Web Tests are designed for handling a wide variety of WebTest scenario. By Using Data Binding, Extraction Rules, Context Parameters we can control our WebTest. But sometimes more control is needed. Coded WebTest Provide a most Control and extensibility because we can code using C# or VB.Net.
In WebTest there is no direct approach to loop our request. If a WebTest must loop in order to issue several requests that are dynamically determined during the test, we can use coded Web tests,
Here I am going to explain a small example of it.
Scenario -
Let assume there a small web application, in that there is login screen, home page and Product Page. Once user provides correct Username and Password in the login screen, he will be redirected to Home Page from there he can go to Product Page. In product page user can enter all the products name he/she wants to buy and then he/she will be clicking on logout.
Below are the two Lists one for Users and one for Products
Users Products
1. Surya 1. Mobile
2. Madhu 2. Computer
3. Laptop
4. TV
5. DSLR Camera
User will login using username “Surya†and in product page he will enter all the products available in the product list and then he will logout. Same sequence will be followed for user “Madhuâ€.
Expected Result
ID UserID Product
1 1 Mobile
2 1 Computer
3 1 Laptop
4 1 TV
5 1 DSLR Camera
6 2 Mobile
7 2 Computer
8 2 Laptop
9 2 TV
10 2 DSLR Camera
Record Parent WebTest
1. Start Recording
2. Open Login Page
3. Enter UserName and Password
4. Click Login
5. Click on Product link to navigate to Product Page
6. Click logout
7. Stop Recording
Record Child WebTest
1. Start Recording
2. Open Product page
3. Enter a Product Name
4. Click on Add
5. Stop Recoding
Bind username, password and Product textbox to the datasource. Follow below link for more details on data binding.
http://mscoder.wordpress.com/2010/05/19/creating-webtest-in-visual-studio-team-system-2008/
Generate the Code of Both the WebTests -> Right Click on WebTest -> Click Generate Code.
Right Click on the Project and Build it
Open ParentCoded Class
Add IncludeCodedWebTest attribute to ParentCoded Class.
[IncludeCodedWebTest("MSCoderTestProject.ChildCoded", "MSCoderTestProject.dll")]
Change the DataBindingAccessMethod from Sequential to Unique.
Call Child WebTest in ParentWebTest before Logout request.
Eg.
WebTest myChildTest = IncludeWebTest(new MSCoderTestProject.ChildCoded(), false); foreach (WebTestRequest r in myChildTest) { yield return r; }
Open ChildCoded Class
Insert a loop which will execute 5 times.
Move the DataTable Cursor to Next row
int maxCount = 5; for (int index = 1; index <= maxCount; index++) { //Code for making Request if (index != maxCount) { this.MoveDataTableCursor("ProductDataSource", "Products#csv"); } }
Run a Coded WebTest
Click Test Menu
Click Windows -> Click Test List Editor
Select ParentCoded Test and Right Click on it
Click on Run Checked Tests.
Result will be displayed in the Test Result window.
Double Click on the result.
Here we can see all the request made by the ChildCodedTest.
This is the final view of the table where data has been saved by the test.
Link to Download the Code