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.


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

To complete this walkthrough, you need the following:

To prepare for the walkthrough

  1. Open the Web application you created in Walkthrough: Creating a Simple Web Application.

  2. Press CTRL+F5 to run the Web application in the browser. You should see the first page. Close the browser.


    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.

  3. Open the Web application you created in Walkthrough: Recording and Running a Web Test.

  4. In Solution Explorer, double-click ColorWebTest.webtest. The Web Test Editor appears and the list of Web requests is displayed.

To convert an existing Web test to a coded Web test

  1. Click the Generate Code button on the toolbar in the Web Test Editor.

  2. Accept the default name in the dialog box and click OK.


    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.

  3. On the Build menu, click Build Solution.

    The new code compiles.

To add code to a Web test

  1. 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.

  2. 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;
        WebTestRequest customRequest = new WebTestRequest("http://msdn.microsoft.com/");
        yield return customRequest;

    If DateTime.Today.DayOfWeek = DayOfWeek.Friday Then
        Dim customRequest As WebTestRequest = New WebTestRequest("http://weather.msn.com/")
        Dim customRequest As WebTestRequest = New WebTestRequest("http://msdn.microsoft.com/")
    End If

  3. On the Build menu, click Build Solution.

To verify the Web test

  1. On the Test menu, click Windows and then click Test View.

  2. Right-click ColorWebTestCoded and select Run Selection.


    The progress of a coded Web test can be viewed just like a recorded Web test.

  3. In the Test Results window, right-click the test results and choose View Test Results Details.

  4. 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.

Community Additions