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.
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 separate Class library project in which to store your Web test and a Web test plug-in.
-
Select the new 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 Project tab, select the new class library. Click OK.
-
Write code in the new class that derives from WebTestPlugin. You must write the additional code to implement the methods.
-
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