Walkthrough: Creating a Coded Web Test
In this walkthrough you will convert a recorded Web test into a coded Web test.
This walkthrough steps you through the conversion of an existing, recorded Web test into a coded Web test. A recorded Web test begins as a list of URLs that represent Web requests. A Web test can be converted to a code-based script. After a Web test has been converted to its coded format, looping and branching constructs can be added. After you convert the Web test to a coded Web test, you can edit that code like any other source code.
Note
You should add validation rules, extraction rules, and data binding to your Web test before you convert it to a coded Web test. You can add these to your coded Web test but, if you use the editor to add them first, you will save you time and sample code will be generated for you when you convert the test.
In this walkthrough, you will perform the following tasks:
Create a coded Web test by converting a recorded Web test
Add new code to a Web test
Run the Web test to verify that it works correctly
Prerequisites
To complete this walkthrough, you need the following:
Visual Studio Team System Test Edition
The Web application that you created in Walkthrough: Creating a Simple Web Application
The Web test that you created in Walkthrough: Recording and Running a Web Test.
Preparing for the Walkthrough
To prepare for the walkthrough
Open the Web application you created in Walkthrough: Creating a Simple Web Application.
Press CTRL+F5 to run the Web application in the browser. You should see the first page. Close the browser.
Note
This starts the ASP.Net Development Server that runs the Web application your Web test will test. You will see the ASP.Net Development Server icon in the notification area.
Open the Web application you created in Walkthrough: Recording and Running a Web Test.
In Solution Explorer, double-click ColorWebTest.webtest. The Web Test Editor appears and the list of Web requests is displayed.
Creating a Coded Web Test
To convert an existing Web test to a coded Web test
Click the Generate Code button on the toolbar in the Web Test Editor.
Accept the default name in the dialog box and click OK.
Note
The default name will be the name of the test with the word Coded appended.
A new file is created. A new window opens and the source code for the test is displayed.
On the Build menu, click Build Solution.
The new code compiles.
Adding Code to a Web Test
To add code to a Web test
Locate the Run() method if your test is in Visual Basic or the GetRequestEnumerator() method if your test is in C#. You will see code corresponding to each Web request in the test.
Scroll down to the end of the method, and after the code for the last Web request, add the following code:
if (DateTime.Today.DayOfWeek == DayOfWeek.Friday) { WebTestRequest customRequest = new WebTestRequest("http://weather.msn.com/"); yield return customRequest; } else { WebTestRequest customRequest = new WebTestRequest("https://msdn.microsoft.com/"); yield return customRequest; }
If DateTime.Today.DayOfWeek = DayOfWeek.Friday Then Dim customRequest As WebTestRequest = New WebTestRequest("http://weather.msn.com/") MyBase.Send(customRequest) Else Dim customRequest As WebTestRequest = New WebTestRequest("https://msdn.microsoft.com/") MyBase.Send(customRequest) End If
On the Build menu, click Build Solution.
Running the Web Test to Verify that it Works Correctly
To verify the Web test
On the Test menu, click Windows and then click Test View.
Right-click ColorWebTestCoded and select Run Selection.
Note
The progress of a coded Web test can be viewed just like a recorded Web test.
In the Test Results window, right-click the test results and choose View Test Results Details.
In the Web Test Viewer, in the top pane, scroll to the last Web request. Verify that the correct Web site was chosen based on the current day.
See Also
Tasks
How to: Create a Coded Web Test
Walkthrough: Creating a Simple Web Application
Walkthrough: Recording and Running a Web Test
Walkthrough: Adding Data Binding to a Web Test
Walkthrough: Adding Validation and Extraction Rules to a Web Test