Setting request properties with a Web Test Plug-in

This blog post will show you how to write a plug-in to set properties on each request. For example, you might want to reduce all think times by 10% or change the timeout values for a request or turn off dependent parsing. These are just a few examples but as you can imagine, you would not want to have to go to each request and change the values. Especially if you are going to want to change it back to the original value. You can accomplish these types of activities by creating a web test plug-in. Here is a link to help documentation on web test plug-ins: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.webtestplugin.aspx

A web test plug-in allows you to override the following methods:

1) PreWebTest

2) PostWebTest

3) PreRequest

4) PostRequest

5) PrePage

6) PostPage

7) PreTransaction

8) PostTransaction

 

We are going to create a simple plug-in which will set the ParseDependentsRequest value for each request and to do this we will override the PreRequest method. Do the following:

1) Add a class file to a test project.

2) Copy the below class and paste it into the class file:

using System;

using System.ComponentModel;

using Microsoft.VisualStudio.TestTools.WebTesting;

 

namespace TestProject1

{

    [Description("This plugin can be used to set the ParseDependentsRequests property for each request")]

    public class ParseDependentsPlugin : WebTestPlugin

    {

        private bool m_parseDependents = true;

        public override void PreRequest(object sender, PreRequestEventArgs e)

        {

            //set the ParseDependentsRequests value on the request

            e.Request.ParseDependentRequests = m_parseDependents;

        }

 

        [DefaultValue(true)]

      [Description("All requests will have their ParseDependentsRequests property set to this value")]

        public bool ParseDependents

        {

            get

            {

                return m_parseDependents;

            }

            set

            {

                m_parseDependents = value;

            }

        }

    }

}

3) Compile

4) Go to webtest editor and right click on the root node an select Add Web test plugin...

5) You should see this plugin. One of the things that the above plug-in does is expose a property that can be seen in the Add Web Test Plugin dialog. As you can see, we can give it a default value and a description.

6) Set the value of the ParseDependents and choose Ok.

7) Run the test.

You should see that dependents were either parsed or not depending on what you selected. You could easily extend this by adding more properties and having one plug-in used to configure multiple properties of each request.