How to: Create a Recorder Plug-In
The WebTestRecorderPlugin lets you modify a recorded Web performance test. The modification occurs after you choose Stop in the Web performance test recorder toolbar but prior to the test being saved and presented in the Web Performance Test Editor.
The primary issue a recorder plug-in helps resolve is performing your own custom correlation on dynamic parameters. With the built-in correlation functionality, Web performance tests detect the dynamic parameters in the Web recording upon completion, or when you use the Promote Dynamic Parameters to Web Test Parameters on the Web Performance Test Editor toolbar. For more information, see How to: Resolve Web Performance Test Playback Issues Caused by Dynamic Parameters. However, the built in detection functionality does not always find all the dynamic parameters. For example, it does not find a session ID, which usually gets its value changed between 5 to 30 minutes. Therefore, you have to manually perform the correlation process. For more information, see How to: Promote Dynamic Parameters to Web Performance Test Parameters.
The WebTestRecorderPlugin lets you write code for your own custom plug-in. This plug-in can perform correlation or modify the Web performance test in many ways prior to it being saved and presented in the Web Performance Test Editor. Therefore, if you determine that a specific dynamic variable has to be correlated for a lot of your recordings, you can automate the process.
Some other ways that a recorder plug-in can be used is for adding extraction and validation rules, adding context parameters, or converting comments to transactions in a Web performance test.
The following procedures describe how to create the rudimentary code for a recorder plug-in, deploy the plug-in and execute the plug-in. The sample code following the procedures demonstrates how to use Visual C# to create a custom dynamic parameter correlation recorder plug-in.
Requirements
- Visual Studio Ultimate
Creating a Recorder Plug-in
To create a recorder plug-in
Open a solution that contains the Web performance and load test project with the Web performance test for which you want to create a recorder plug-in.
In Solution Explorer, right-click the solution, select Add, and then choose New Project.
The Add New Project dialog box is displayed.
Under Installed Templates, select Visual C#.
In the list of templates, select Class Library.
In the Name text box, type a name for the recorder plug-in.
The class library is added to the Solution Explorer and the new class is opened in the Code Editor.
In Solution Explorer, in the new class library project folder, right-click the References folder and select Add Reference.
Tip
An example of a new class library project folder is RecorderPlugins.
The Add Reference dialog box is displayed.
Select the .NET tab.
Scroll down and select Microsoft.VisualStudio.QualityTools.WebTestFramework and then choose OK.
The Microsoft.VisualStudio.QualityTools.WebTestFramework is added in the References folder in Solution Explorer.
Write the code for your recorder plug-in. First, create a new public class that derives from WebTestRecorderPlugin.
Override the PostWebTestRecording method.
public class Class1 : WebTestRecorderPlugin { public override void PostWebTestRecording(object sender, PostWebTestRecordingEventArgs e) { base.PostWebTestRecording(sender, e); } }
The event arguments will give you two objects to work with: the recorded result and the recorded Web performance test. This will allow you to iterate through the result looking for certain values and then jump to the same request in the Web performance test to make modifications. You can also just modify the Web performance test if you wanted to add a context parameter or parameterize parts of the URL.
Note
If you do modify the Web performance test, you will also need to set the RecordedWebTestModified property to true: e.RecordedWebTestModified = true;
Add more code according to what you want the recorder plug-in to execute after the Web recording occurs. For example, you can add code to handle custom correlation as shown in the sample below. You can also create a recorder plug-in for such things as converting comments to transactions or adding validation rules to the Web performance test.
On the Build menu, choose Build <class library project name>.
Next, you must deploy the recorder plug-in in order for it to register with Visual Studio Ultimate.
Deploy the Recorder Plug-in
After you compile the recorder plug-in, you will need to place the resulting DLL in one of two locations:
%ProgramFiles%\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\WebTestPlugins
%USERPROFILE%\My Documents\Visual Studio 11\WebTestPlugins
Warning
After you copy the recorder plug-in to one of the two locations, you must restart Visual Studio Ultimate for the recorder plug-in to be registered.
Execute the Recorder Plug-in
When you create a new Web performance test, you will see the Enable WebTestRecordPlugins dialog box. The Enable WebTestRecordPlugins dialog box displays all of the available plug-ins that can be executed.
To execute the recorder plug-in
Create a new Web performance test. For more information, see How to: Create a New Web Performance Test Using the Web Performance Test Recorder.
The Enable WebTestRecordPlugins dialog box displays.
Select the check box for the recorder plug-in and choose OK.
After the Web performance test completes recording, the new recorder plug-in will be executed.
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
This sample demonstrates how to create a customized Web performance test recorder plug-in to perform custom dynamic parameter correlation.
Note
A complete listing of the sample code is located at the bottom of this topic.
Reviewing the Sample Code
Iterate through the result to find first page with ReportSession
This part of the code sample iterates through each recorded object and searches the response body for ReportSession.
foreach (WebTestResultUnit unit in e.RecordedWebTestResult.Children)
{
WebTestResultPage page = unit as WebTestResultPage;
if (page != null)
{
if (!foundId)
{
int indexOfReportSession = page.RequestResult.Response.BodyString.IndexOf("ReportSession");
if (indexOfReportSession > -1)
{
Add an extraction rule
Now that a response has been found, you need to add an extraction rule. This part of the code sample creates the extraction rule using the ExtractionRuleReference class and then finds the correct request in the Web performance test to add the extraction rule to. Each result object has a new property added called DeclarativeWebTestItemId which is what is being used in the code to get correct request from the Web performance test.
ExtractionRuleReference ruleReference = new ExtractionRuleReference();
ruleReference.Type = typeof(ExtractText);
ruleReference.ContextParameterName = "SessionId";
ruleReference.Properties.Add(new PluginOrRuleProperty("EndsWith", "&ControlID="));
ruleReference.Properties.Add(new PluginOrRuleProperty("HtmlDecode", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("IgnoreCase", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("Index", "0"));
ruleReference.Properties.Add(new PluginOrRuleProperty("Required", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("StartsWith", "ReportSession="));
ruleReference.Properties.Add(new PluginOrRuleProperty("UseRegularExpression", "False"));
WebTestRequest requestInWebTest = e.RecordedWebTest.GetItem(page.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
requestInWebTest.ExtractionRuleReferences.Add(ruleReference);
e.RecordedWebTestModified = true;
}
Replace query string parameters
Now the code finds all the query string parameters that have ReportSession as name and change the value to {{SessionId}} as shown in this part of the code sample:
WebTestRequest requestInWebTest = e.RecordedWebTest.GetItem(page.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
foreach (QueryStringParameter param in requestInWebTest.QueryStringParameters)
{
if (param.Name.Equals("ReportSession"))
{
param.Value = "{{SessionId}}";
}
}
}
using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
namespace RecorderPlugin
{
[DisplayName("Correlate ReportSession")]
[Description("Adds extraction rule for Report Session and binds this to querystring parameters that use ReportSession")]
public class CorrelateSessionId : WebTestRecorderPlugin
{
public override void PostWebTestRecording(object sender, PostWebTestRecordingEventArgs e)
{
//first find the session id
bool foundId = false;
foreach (WebTestResultUnit unit in e.RecordedWebTestResult.Children)
{
WebTestResultPage page = unit as WebTestResultPage;
if (page != null)
{
if (!foundId)
{
int indexOfReportSession = page.RequestResult.Response.BodyString.IndexOf("ReportSession");
if (indexOfReportSession > -1)
{
//add an extraction rule to this request
// Get the corresponding request in the Declarative Web performance test
ExtractionRuleReference ruleReference = new ExtractionRuleReference();
ruleReference.Type = typeof(ExtractText);
ruleReference.ContextParameterName = "SessionId";
ruleReference.Properties.Add(new PluginOrRuleProperty("EndsWith", "&ControlID="));
ruleReference.Properties.Add(new PluginOrRuleProperty("HtmlDecode", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("IgnoreCase", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("Index", "0"));
ruleReference.Properties.Add(new PluginOrRuleProperty("Required", "True"));
ruleReference.Properties.Add(new PluginOrRuleProperty("StartsWith", "ReportSession="));
ruleReference.Properties.Add(new PluginOrRuleProperty("UseRegularExpression", "False"));
WebTestRequest requestInWebTest = e.RecordedWebTest.GetItem(page.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
requestInWebTest.ExtractionRuleReferences.Add(ruleReference);
e.RecordedWebTestModified = true;
}
foundId = true;
}
}
else
{
//now update query string parameters
WebTestRequest requestInWebTest = e.RecordedWebTest.GetItem(page.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
foreach (QueryStringParameter param in requestInWebTest.QueryStringParameters)
{
if (param.Name.Equals("ReportSession"))
{
param.Value = "{{SessionId}}";
}
}
}
}
}
}
}
}
}
See Also
Tasks
How to: Create a Coded Web Performance Test
How to: Edit an Existing Web Performance Test Using the Web Performance Test Editor
Reference
Other Resources
Creating and Using Custom Plug-ins for Load and Web Performance Tests