How to skip steps in a Visual Studio Web Test

In Visual Studio Web Test there are cases when you do not want your web test to not execute certain web requests in the list based on some run time condition. This skip may be required because if certain conditions are not met from prior requests then executing that step will end up in violating extraction rules and then results for your web test will show up errors.

If the execution for a step is going to be skipped based on some prior request, then you are going to need to store the trigger or flag in some context parameter for your web test. And then in pre request step of your request, you can check for value of that context parameter to decide if step needs to be skipped. Lets how will we go about setting this up.

Set up web test

Set up your web test with all the requests that need to be issued as part of your test plan. For example I have created a simple web test for demo. See that I have inserted a step to call msn.com in the middle. Yes, this is what I am going to skip.

Set up Context Parameter For Web Test

Now set up a context parameter for your web test. Depending on your test plan and steps, you can choose at what step and under what conditions value for this context parameter is going to be set. For example I have added a custom extraction rule in first step of the test plan. The code for this extraction rule looks as below. For demo purpose I have just set value of my conext parameter to a hard coded string. Implementation of this extraction rule will depend on your business rules.

public override void Extract(object sender, ExtractionEventArgs e)
{
  if (e.Response.HtmlDocument != null)
  {
     // Peform extraction of data from response to set the context
     // parameter value here.
     e.WebTest.Context.Add(this.ContextParameterName, "Extracted!");
     e.Success = true;
  }
  e.Success = true;
}

Check For Context Parameter Prior To Request

Next I have added a Request Plugin for my next step where request for MSN.COM is going to be executed. I am overriding PreRequest method for the plugin to perform test for context parameter in web test. Based on the results of this test, I have set Instruction property of this step to Skip.

public override void PreRequest(object sender, PreRequestEventArgs e)
{
   // Check if we have our special context parameter value
   // set for skipping a step.
   object ctxVal = null;
   if (e.WebTest.Context.TryGetValue("eBayInput", out ctxVal))
   {
      if (string.Compare(ctxVal as string, "Extracted!", true) == 0)
      {
        if (e.Request.Url.ToLower().Contains("msn.com"))
        {
           e.Instruction = WebTestExecutionInstruction.Skip;
        }
      }
   }
   base.PreRequest(sender, e);
}

Results Of Web Test

As you can see from the screenshot below, the test step for MSN.COM was not executed!

visual studio web test result

If you any questions or need help with Visual Studio automated testing, shoot me a message.

comments powered by Disqus

Blog Tags