How to: Create a Web Test Plug-In

Web tests plug-ins provide a way for you to isolate some code outside the main declarative statements in your Web test. A customized Web test plug-in offers you a way to call some code as the Web test is run. The Web test plug-in is run once for every test iteration.

You can create a customized Web test plug-in by deriving your own class from the WebTestPlugin base class.

You can use customized Web test plug-ins with the Web tests you have recorded, which allows you to write a minimal amount of code to attain a greater level of control over your Web tests. However, you can also use them with coded Web tests. For more information, see How to: Create a Coded Web Test.

Note

You can also create load test plug-ins. For more information, see How to: Create a Load Test Plug-In.

To create a custom Web test plug-in

  1. Open a Test Project that contains a Web test.

    For more information about how to create a test project, see How to: Create a Test Project.

  2. Create a separate Class library project in which to store your Web test and a Web test plug-in.

  3. Select the new project and then right-click Add Reference.

  4. On the .NET tab, select Microsoft.VisualStudio.QualityTools.WebTestFramework. Click OK.

  5. In your Test Project, right-click and select Add Reference. On the Project tab, select the new class library. Click OK.

  6. Write code in the new class that derives from WebTestPlugin. You must write the additional code to implement the methods.

  7. After you have written the code, build the new project.

  8. Open a Web test.

  9. To add the Web test plug-in, click Set Web Test Plug-in on the toolbar. This displays your test plug-in in the Set Web Test Plug-in dialog box. Select your class and then click OK.

    Note

    You can also change the Web test plug-in in the Properties window. Select the Web test node and press F4. In the Properties window, you see the Plug-in category and the plug-ins you have added to the Web test.

Example

The following code creates a customized Web test plug-in that adds an item to the WebTestContext that represents the test iteration.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace SampleRules
{
    public class SampleWebTestPlugin : WebTestPlugin
    {
        // start counting iterations at 1 not 0
        // so that the iteration number we give matches the run number
        static int testIterationNumber = 1;

        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
        }

        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            e.WebTest.Context["TestIterationNumber"] = testIterationNumber;
            testIterationNumber++;
        }
    }
}

See Also

Tasks

How to: Create a Web Test Request Plug-In
How to: Create a Custom Extraction Rule
How to: Create a Custom Validation Rule
How to: Create a Load Test Plug-In
How to: Create a Coded Web Test
How to: Edit an Existing Web Test

Reference

WebTestRequestPlugin