Share via


Stopping a webtest on a request error

This post provides a sample WebTestRequestPlugin that shows you how to stop a web test when an error occurs. Often when a test has an error, the remainder of requests will either fail or not do what was intended. If they fail, then it might be difficult to pinpoint the exact problem with your web test because the test may have many errors. Another factor where this can cause a problem is when you are running your tests within a load test. The load test will report many errors, most of which are misleading. The load test will also stop collecting errors after a certain number of errors have been hit.

 

   A WebTestRequestPlugin is a block of code which can be executed right before a request is submitted or right after a request is submitted. Here is a link with more help on this kind of plug-in: WebTestRequestPlugin Help. This example plug-in will stop a test on 2 different conditions. First, if there is no response returned. This may happen if you have custom code that is failing. For example, if you try to bind a query string parameter to a parameter in the WebTestContext, but the parameter does not exist in the context. The second condition for stopping the test is if the http status code is in the 400 or 500 range.

 

Here is the plug-in code:

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebTestExamples

{

    public class StopTestPlugin : WebTestRequestPlugin

    {

        public override void PostRequest(object sender,

                                         PostRequestEventArgs e)

        {

            //Check 2 conditions:

            // 1) If there is no response, that usually indicates an

            // error prior to even sending a request. For

            // example, binding to a context parameter that does

            // not exist.

            //

            // 2) Check the return code. If it is in the 400 or 500

            // range, then stop the test.

            if (!e.ResponseExists

                    || ( ((int)e.Response.StatusCode) >= 400))

            {

                e.WebTest.Stop();

            }

        }

        public override void PreRequest(object sender,

                                        PreRequestEventArgs e)

        {

                       

        }

    }

}

 

To use this plug-in, do the following:

1) Add this class to a test project

2) Compile the class

3) Open a web test

4) Click on the web test toolbar item for Set Request Plug-in…

5) Select the new plug-in in the dialog which opens.

6) Save and run a test.

Now if a request hits an error, the test should stop. When you open the result for the web test, the request which hit the error should always be the last request in the result. You will not see the requests which were not executed. Hopefully this will make it easier to identify the exact request which is causing your test to error.

Comments

  • Anonymous
    March 06, 2016
    How can you stop a web test if an error occurred during preRequest plugin?