Export (0) Print
Expand All

How to: Create a Request-Level Plug-In

Requests are the declarative statements that constitute Web tests. Web tests plug-ins enable you to isolate and reuse code outside the main declarative statements in your Web test. You can create plug-ins and add them to an individual request as well as to the Web 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 test.

NoteNote:

In Visual Studio 2005 Team Edition for Testers, it was possible to create a request plug-in at the test level that ran for every request in the test. In Team System 2008 Test Edition, test-level plug-ins support the request-plug-in interface and will apply to every request in the test. When Team System 2008 Test Edition encounters a request plug-in that was created in Visual Studio 2005, it converts the plug-in to an equivalent test-level plug-in.

Every Web 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 recieved.

You can create a customized Web test request plug-in by deriving your own class from the WebTestRequestPlugin base class.

You can use customized Web test request plug-ins with the Web tests you have recorded. Customized Web test request plug-ins enable 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 request-level plug-in

  1. 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.

  2. In the same solution, create a class library project in which to store your request plug-in.

  3. Select the class library project and then right-click Add Reference.

  4. On the .NET tab, select Microsoft.VisualStudio.QualityTools.WebTestFramework. Click OK.

  5. In your test project, right-click and select Add Reference.

  6. On the Projects tab, select the new class library project. Click OK.

  7. Write the code of your plug-in. First, create a new public class that derives from WebTestRequestPlugin.

  8. Implement code inside one or both of the PreRequest and PostRequest event handlers.

  9. After you have written the code, build the new project.

  10. Open the Web test to which you want to add the request plug-in.

  11. Right-click the request to which you want to add the plug-in, and then select Add Request Plug-in.

    The Add Web Test Request Plug-in dialog box is displayed.

  12. Under Select a plug-in, select your new plug-in and then click OK.

    The plug-in is added to the Request Plug-ins folder, which is a child folder of the http request.

You can use the following code to create a customized Web test plug-in.

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);
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft