This post walks you through the process of adding multiple plugins to webtests.  First I’ll walk you through adding multiple plugins to a coded test and then one approach for doing this with declarative web tests.  Coded web tests already fully support multiple plugins so it is easy to have a coded test execute multiple test and request plugins.  For example, look at this code snippet from a coded webtest called MultiplePluginExample.

 

  //create the plugins

  private WebTestPlugin1 testPlugin = new WebTestPlugin1();

        private WebTestPlugin2 testPlugin2 = new WebTestPlugin2();

 

        private WebTestRequestPlugin1 requestPlugin = new WebTestRequestPlugin1();

        private WebTestRequestPlugin2 requestPlugin2 = new WebTestRequestPlugin2();

 

        public MultiplePluginExample()

        {

            this.PreAuthenticate = true;

 

            //hook up the webtest plugins

            this.PreWebTest += new EventHandler<PreWebTestEventArgs>(this.testPlugin.PreWebTest);

            this.PostWebTest += new EventHandler<PostWebTestEventArgs>(this.testPlugin.PostWebTest);

            this.PreWebTest += new EventHandler<PreWebTestEventArgs>(this.testPlugin2.PreWebTest);

            this.PostWebTest += new EventHandler<PostWebTestEventArgs>(this.testPlugin2.PostWebTest);

           

            //hook up the webtestrequest plugins

            this.PreRequest += new EventHandler<PreRequestEventArgs>(this.requestPlugin.PreRequest);

            this.PostRequest += new EventHandler<PostRequestEventArgs>(this.requestPlugin.PostRequest);

            this.PreRequest += new EventHandler<PreRequestEventArgs>(this.requestPlugin2.PreRequest);

            this.PostRequest += new EventHandler<PostRequestEventArgs>(this.requestPlugin2.PostRequest);

        }

 

You can see 2 web test plugins and 2 requests plugins being created and connected to the appropriate events.  In a coded web test, you can also have a request plugin for a specific request.  For example, look at this code snippet.

 

WebTestRequest request1 = new WebTestRequest("http://url...");

     request1.ThinkTime = 2;

 

      //connect a request plugin for one request

      WebTestRequestPlugin3 requestPlugin3 = new WebTestRequestPlugin3();

      request1.PreRequest+=new EventHandler<PreRequestEventArgs>(requestPlugin3.PreRequest);

      request1.PostRequest += new EventHandler<PostRequestEventArgs>(requestPlugin3.PostRequest);

 

      yield return request1;

 

Request1 has WebTestReqeustPlugin3 connected to the pre and post request events.  This plugin will only fire for request1.

 

Now if you want to do this in a declarative web test, you could use a web test plugin to connect up the other plugins.  There is one limitation to this approach which is that you cannot have multiple PreWebTest events fire.  So if you have multiple web test plugins, only the PreWebTest event of the ControllerPlugin would fire.  Suppose we have 2 test plugins and 2 request plugins that we want to execute for each test.  The following plugin class shows how you could connect these to the correct events:

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting;

 

namespace TestProject2

{

    public class ControllerPlugin : WebTestPlugin

    {

        public override void PostWebTest(object sender, PostWebTestEventArgs e)

        {

            //do nothing

        }

 

        public override void PreWebTest(object sender, PreWebTestEventArgs e)

        {

            //connect the web test plugins

            WebTestPlugin1 testPlugin1 = new WebTestPlugin1();           

            e.WebTest.PostWebTest += new EventHandler<PostWebTestEventArgs>(testPlugin1.PostWebTest);

 

            WebTestPlugin2 testPlugin2 = new WebTestPlugin2();

            e.WebTest.PostWebTest += new EventHandler<PostWebTestEventArgs>(testPlugin2.PostWebTest);

 

            //connect the request plugns

            WebTestRequestPlugin1 requestPlugin1 = new WebTestRequestPlugin1();

            e.WebTest.PreRequest += new EventHandler<PreRequestEventArgs>(requestPlugin1.PreRequest);

            e.WebTest.PostRequest += new EventHandler<PostRequestEventArgs>(requestPlugin1.PostRequest);

 

            WebTestRequestPlugin2 requestPlugin2 = new WebTestRequestPlugin2();

            e.WebTest.PreRequest += new EventHandler<PreRequestEventArgs>(requestPlugin2.PreRequest);

            e.WebTest.PostRequest += new EventHandler<PostRequestEventArgs>(requestPlugin2.PostRequest);

        }

               

    }

}

 

Again, only the PreWebTest event of the controller plugin would fire.  If you want the PreWebTest event for another test plugin to fire, then you could call the PreWebTest method from the Controller plugins PreWebTest method.  For example,

 

public class ControllerPlugin : WebTestPlugin

    {

        public override void PostWebTest(object sender, PostWebTestEventArgs e)

        {

            //do nothing

        }

 

        public override void PreWebTest(object sender, PreWebTestEventArgs e)

        {

            //connect the web test plugins

            WebTestPlugin1 testPlugin1 = new WebTestPlugin1();                      

            e.WebTest.PostWebTest += new EventHandler<PostWebTestEventArgs>(testPlugin1.PostWebTest);

 

            //call PreWebTest for WebTestPlugin1

            testPlugin1.PreWebTest(this, e);

           

           

 

 

After creating the controller plugin, you would then set this plugin as the web test plugin in the webtest editor.  You would not set the request plugin to anything, because the controller plugin handles connecting the request plugins as well. 

 

Here are some additional help links on web test and request plugins:

http://msdn2.microsoft.com/en-us/library/ms243191(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ms182554(VS.80).aspx