How to: Create a request-level plug-in
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Requests are the declarative statements that constitute web performance tests. Web performance test plug-ins enable you to isolate and reuse code outside the main declarative statements in your web performance test. You can create plug-ins and add them to an individual request as well as to the web performance test that contains it. A customized request plug-in offers you a way to call code as a particular request is run in a web performance test.
Note
Web performance and load test functionality is deprecated. Visual Studio 2019 is the last version where web performance and load testing will be fully available. For more information, see the Cloud-based load testing service end of life blog post. To reduce the impact on users some minimum support is still available in Visual Studio 2022 Preview 3 or later. Users should also note web performance and load test functionality only supports Internet Explorer which is not available on Windows 11 and some versions of Windows 10. Read more on Internet Explorer Lifecycle policy.
Every web performance test request plug-in has a PreRequest method and a PostRequest method. After you attach a request plug-in to a particular http request, the PreRequest event will be fired before the request is issued and the PostRequest fired after the response is received.
You can create a customized web performance test request plug-in by deriving your own class from the WebTestRequestPlugin base class.
You can use customized web performance test request plug-ins with the web performance tests you have recorded. Customized web performance test request plug-ins enable you to write a minimal amount of code to attain a greater level of control over your web performance tests. However, you can also use them with coded web performance tests. See Generate and run a coded web performance test.
To create a request-level plug-in
In Solution Explorer, right-click the solution, select Add and then choose New Project.
Create a new Class Library project.
In Solution Explorer, right-click the References folder in the new class library and select Add Reference.
The Add Reference dialog box is displayed.
Choose the .NET tab, scroll down, select Microsoft.VisualStudio.QualityTools.WebTestFramework and then choose OK
The reference to Microsoft.VisualStudio.QualityTools.WebTestFramework is added to the Reference folder in Solution Explorer.
In Solution Explorer, right-click the top node of the web performance and load test project that contains the load test to which you want to add the web performance test request test plug-in. Select Add Reference.
The Add Reference dialog box is displayed.
Choose the Projects tab, select the Class Library Project and then choose OK .
In the Code Editor, write the code of your plug-in. First, create a new public class that derives from WebTestRequestPlugin.
Implement code inside one or both of the PreRequest and PostRequest event handlers. See the following Example section for a sample implementation.
After you have written the code, build the new project.
Open the web performance test to which you want to add the request plug-in.
Right-click the request to which you want to add the request plug-in, and select Add Request Plug-in.
The Add Web Test Request Plug-in dialog box is displayed.
Under Select a plug-in, select your new plug-in.
In the Properties for selected plug-in pane, set the initial values for the plug-in to use at run time.
Note
You can expose as many properties as you want from your plug-ins; just make them public, settable, and of a base type such as Integer, Boolean, or String. You can also change the web performance test plug-in properties later by using the Properties window.
Choose OK.
The plug-in is added to the Request Plug-ins folder, which is a child folder of the HTTP request.
Warning
You might get an error similar to the following when you run a web performance test or load test that uses your plug-in:
Request failed: Exception in <plug-in> event: Could not load file or assembly '<"Plug-in name".dll file>, Version=<n.n.n.n>, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
This is caused if you make code changes to any of your plug-ins and create a new DLL version (Version=0.0.0.0), but the plug-in is still referencing the original plug-in version. To correct this problem, follow these steps:
- In your web performance and load test project, you will see a warning in references. Remove and re-add the reference to your plug-in DLL.
- Remove the plug-in from your test or the appropriate location and then add it back.
Example
You can use the following code to create a customized web performance test plug-in that displays two dialog boxes. One dialog box displays the URL that is associated with the request to which you attach the request add-in. The second dialog box displays the computer name for the agent.
Note
The following code requires that you add a reference to System.Windows.Forms.
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);
}
}
}