Udostępnij za pośrednictwem


Walkthrough: Adding Data Binding to a Web Test (XML File)

In this walkthrough, you will bind a Web test to an XML file and verify that it works correctly.

By using data binding, a database can provide data for a Web Test. You can bind data from a data source to a part of a Web request that requires data, such as a form post parameter. For more information, see About Data Binding in Web Tests.

In this walkthrough, you will perform the following tasks:

  • Add data binding to a Web test

  • Run the Web test to verify that it works correctly

Prerequisites

To complete this walkthrough, you need the following:

Preparing for the Walkthrough

For this walkthrough, you will need a sample data source to bind to. You will create a simple XML file data source.

To prepare the data source

  1. Create a new folder and name it Data.

  2. In the Data folder, create a new text file and name it ColorData.xml.

    Note

    You can use any text editor, for example Notepad, to create the file.

  3. Add the following to the text file:

    <?xml version="1.0"?>
    <ColorData>
        <Color>
            <ColorId>0</ColorId>
            <ColorName>Red</ColorName>
        </Color>
        <Color>
            <ColorId>1</ColorId>
            <ColorName>Blue</ColorName>
        </Color>
    </ColorData>
    
  4. Save and close the file.

    Tip

    You can open the file in an XML editor and confirm that the XML is well-formed.

To prepare the Web test

  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 will 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, at the far right of the taskbar.

  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.

Add Data Binding to a Web Test

To add the data source to the Web test

  1. In the Web Test Editor, right-click the top node of your Web test and then click Add Data Source.

    The New Test Data Source Wizard appears.

  2. In the Data source name box type ColorsXML.

  3. In the Data source type list click XML File.

  4. Click Next.

  5. In the Choose an XML file box enter the path and name of the file, or click the ellipsis to browse to the file. For example, enter the following:

    <Your Path>\Data\ColorData.xml

  6. In the Table drop-down box select Color.

    The data from the XML file appears in the Preview data pane.

  7. Click Next.

  8. Under Choose from the available tables for this data source, select the checkbox next to Color.

  9. Click Finish.

    A dialog appears asking you if you want to add the file to your current project.

  10. Click Yes.

    A Data Sources node will be added to your Web test, and the text file will appear as a table in the hierarchy.

    For the purposes of this walkthrough, it is not necessary to add the file to the project. In the future, you can use the following information to help you decide:

    Response

    Result

    Advantage

    Yes

    The file is copied to the project.

    When the project is deployed, there is no extra work required.

    No

    The file is not copied to the project. When the project is deployed, you might have to update the path of the file.

    Some data files can be very large, and should be maintained separate from the project. Some data files must be shared among several members of a team, and should be maintained in a central location that all members can access.

  11. On the Filemenu click Save ColorWebTest.webtest to save the Web test.

To add data binding to the Web test

  1. In the Web Test Editor, find the request that redirects to the page Red.aspx. This should be the second node in the request list.

    Important noteImportant Note:

    The Web application uses a redirect to move from the page Default.aspx to the page Red.aspx. In the Web Test Editor, the request list will show Default.aspx, not Red.aspx, for the request you are trying to find. To find the correct request, expand the request node, expand the Form Post Parameters folder, and confirm that there is an entry RadioButtonList=Red.

  2. Select the RadioButtonList1 node.

  3. In the Properties window, find the Value property. It is currently set to Red because that is what you selected when you recorded the Web test. Click the property, and then click the down arrow that appears.

  4. Expand ColorsXML, expand the Color table, and then select ColorName. The RadioButtonList is now bound to the data source.

  5. On the Test menu point to Edit Test Run Configurations, and then click Local Test Run.

  6. In the localtestrun.testrunconfig dialog box, select Web Test in the left pane, and then select the One run per data source row option in the right pane. This causes the test to run iteratively, one time for each row in the database. On each test iteration, the cursor moves to the next row in the database.

  7. Click Close.

  8. On the Filemenu click Save ColorWebTest.webtest to save the Web test.

Running the Web Test to Verify that it Works Correctly

To verify the Web test

  1. In the Web Test Editor, click Run Test to start your Web test and display the Web Test Viewer.

  2. In the Web Test Viewer there will be two runs of the Web test. Run 1 will use the first row of data, and Run 2 will use the second row of data. Notice that Run 1 visits the page Red.aspx, and Run 2 visits the page Blue.aspx.

    Note

    You might have to scroll up in the Web Test Viewer to see both test runs.

Next Steps

In this walkthrough you added data binding to a Web test, using an XML file. To learn about binding to other data sources, see the following:

How to: Add Data Binding to a Web Request

Walkthrough: Adding Data Binding to a Web Test (CSV File)

Walkthrough: Adding Data Binding to a Web Test

See Also

Other Resources

Data Binding in Web Tests

Web Test Walkthroughs