Sample of Using the Visual Studio WebTest DeclarativeWebTestSerializer

DeclarativeWebTestSerializer Class

MSDN Documentation, Namespace: Microsoft.VisualStudio.TestTools.WebTesting

The idea for this stems from a blog entry that I found written by fellow Microsoft Employee Dennis Stone.

How many folks are using the DeclarativeWebTestSerializer to help them develop Visual Studio Team System WebTests?

After looking through this and the MSDN documentation I think you will agree that this offers a very powerful extensibility feature that I believe many test developers are unaware of. This class allows you open and then manipulate your .webtest files programatically, then save the result back to a .webtest file again. This allows you to do some very powerful manipulation on the .webtest without having to resort to a coded webtest format, or developing a WebTestPlugin. Imagine being able to open your .webtest file, and then programmatically manipulate and set the common properties that you set for your work environment or the kinds of tests that you build, then save it back to a .webtest with all the settings adjusted.

This approach would be especially useful if you were building sets of .webtests for an application over the product lifecycle, or you found yourself making the same edits to the .webtest file over and over again. You can capture common patterns,properties that you can implement in the processor and will save you time and add consistency to your test development process.

I decided that I would create a quick code sample to show how you could use this built in goodness to do processing on your DeclarativeWebTests. This example demonstrates using the DeclarativeWebTestSerializer to .Open , manipulate using the WebTest object model programmatically, and .Save a .webtest file. With this example, you can see how to create a simple console application that serves to process any .webtest that you develop.

 

The following sample demonstrates the use of the DeclarativeWebTestSerializer.

using System;

using Microsoft.VisualStudio.TestTools.WebTesting;

namespace DeclarativeWebTestProcessor

{

    /// <summary>

    /// Sample code for application to process a .webtest file and sets common properties to values required.

    /// This example demonstrates the power of using the DeclarativeWebTestSerializer to open , modify , and save

    /// a .webtest file.

    /// With this example, you could create a processor that you use for all of your .webtest that

    /// you develop. This would be especially useful if you were building sets of .webtests for an application

    /// over the product lifecycle. Common patterns,properties can be implemented in the processor and will save

    /// you time and add consistency to your test development process.

    /// The idea for this stems from a blog entry that I found written

    /// by Dennis Stone (https://blogs.msdn.com/densto/pages/declarativewebtest-declarativewebtestserializer.aspx)

    /// MSDN Documentation: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.declarativewebtestserializer.aspx

    ///

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            if (args.Length <= 0)

            {

                Console.WriteLine("File to process is required");

                Console.WriteLine(@"Usage: c:\DeclarativeWebTestProcessor.exe InputFilePath [OutputFilePath]");

                Console.WriteLine(@"Example: c:\DeclarativeWebTestProcessor.exe c:\webtest1.webtest c:\webtest1_Processed");

                return;

            }

            //open our .webtest file for processing

            DeclarativeWebTest webtest = DeclarativeWebTestSerializer.Open(args[0]);

            //Adds a new comment at the beginnig of the test

            webtest.Items.Insert(0, (new Comment("WebTest was processed with Automatic File Proccessing:"

                + DateTime.Now.ToString()) ));

            //we need to increase the ResponseBodyCaptureLimit by 4k

            webtest.ResponseBodyCaptureLimit += 4096;

           

            //setup our proxy to use same as Internet Explorer

            webtest.Proxy = "default";

            //set the webtest to StopOnError

            webtest.StopOnError = true;

            //add a custom context parameter to the test

            webtest.ContextParameters.Add(new ContextParameter("TestContextParam","Custom Context Param"));

            //modify some common properites of the requests and modify specific types of requests

            foreach (WebTestItem item in webtest.Items)

            {

                //item could be any WebTestItem (Comment, IncludedWebTest, WebTestRequest, TransactionTimer)

                if (item is WebTestRequest)

                {

                    //we cannot modify the 'item' directly in a foreach

                    WebTestRequest itemReq = item as WebTestRequest;

                    //clear the ExpectedResponseUrl for all requets

                    itemReq.ExpectedResponseUrl = string.Empty;

                    //set response time goal for SLA requirement = 10 seconds or less

                    itemReq.ResponseTimeGoal = 10;

                    //if our HTTP Method (GET,POST,HEAD,etc) is a POST Operation

                    if (itemReq.Method.Equals("Post", StringComparison.InvariantCultureIgnoreCase))

                    {

                        //SLA - all response times for Post operations must complete 25 seconds or less

   itemReq.ResponseTimeGoal = 25;

                    }

                    //if our request url is for the msdn.microsoft.com

                    //we need to setup a few more properties specificly

                    if (itemReq.Url.ToLower().Contains("msdn.microsoft.com"))

                    {

                        //setup a think time of 45 seconds

                        itemReq.ThinkTime = 45;

                        //turn off parsing of dependent requests

                       itemReq.ParseDependentRequests = false;

                        //set a timeout of 400 seconds

                        itemReq.Timeout = 400;

                    }

                }

            }

          

            //specify a default filename in case they did not provide

            string fileNameSave = webtest.Name + "_Proccessed_" + DateTime.Now.ToFileTime().ToString() + ".webtest";

           

            //if they specified a filename then we will use that instead

            if (args.Length > 1)

            {

                fileNameSave = args[1];

            }

            //now save our test

            DeclarativeWebTestSerializer.Save(webtest, fileNameSave);

        }

    }

}