How to: Create a Web Test Request Plug-In
Requests are the declarative statements that constitute Web tests. Request plug-ins provide a way for you to isolate code outside the main individual requests in your Web test. A customized request plug-in offers you a way to call code as each request is run in a Web test. You can create a customized request plug-in by deriving your own class from the WebTestRequestPlugin base class.
A customized request plug-in can be used with any test you have recorded. After you have recorded a Web test, you can examine and edit requests in the Web Test Editor. For more information, see How to: Edit an Existing Web Test.
Additionally, you can also use customized request plug-ins with coded Web tests. For more information, see How to: Create a Coded Web Test.
To create a customized request 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.
-
In the same solution, create a separate Class library project in which to store your request plug-in.
-
Select the new 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 Project tab, select the new class library. Click OK.
-
Write your code in the new class that derives from WebTestRequestPlugin. You must write the additional code inside either the PreRequest or PostRequest event handlers.
-
After you have written the code, build the new project.
-
Open the Web test to which you want to add the request plug-in.
-
To add a custom request, click Set Request Plug-in on the toolbar. This displays your request plug-in in the Set Request Plug-in dialog box. Select the class and then click OK.
Note You can also change the request 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 request plug-in that accesses information found within the WebTestContext and the WebTestResponse.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace RequestPluginNamespace
{
public class MyWebRequestPlugin : WebTestRequestPlugin
{
public override void PostRequest(object sender, PostRequestEventArgs e)
{
MessageBox.Show(e.WebTest.Context.AgentName);
}
public override void PreRequest(object sender, PreRequestEventArgs e)
{
MessageBox.Show(e.Request.Url);
}
}
}
