How to: Create a Web Test Plug-In
Web tests plug-ins enable you to isolate and reuse 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 one time for every test iteration. In addition, if you override the PreRequest or PostRequest methods in the test plug-in, those request plug-ins will run before or after each request, respectively.
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 enables 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
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.
Create a class library project in which to store your Web test and a Web test plug-in.
Select the class library project and then right-click Add Reference.
On the .NET tab, select Microsoft.VisualStudio.QualityTools.WebTestFramework. Click OK.
In your test project, right-click and select Add Reference.
On the Projects tab, select the new class library. Click OK.
Write the code of your plug-in. First, create a new public class that derives from WebTestPlugin.
Implement code inside one or both of the PreWebTest and M:Microsoft.VisualStudio.TestTools.WebTesting.WebTestPlugin.PostWebTest(System.Object,Microsoft.VisualStudio.TestTools.WebTesting.PostWebTestEventArgs) event handlers.
After you have written the code, build the new project.
Open a Web test.
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 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