Jaa


Visual Studio WebTest Validation Rules - Built-in Response URL rule is case sensitive...

 The built in Validation Rule, called Response URL is case sensitive, and this rule is to help validate that a page that was redirected to another page during recording, also honors the same when playing back via a VS WebTest. The problem is that some web apps don’t always respond with urls that are same case as what was recorded, especially if the data used to drive the test might have a virtual user exercise a slightly different code path. So a URL to https://mysite/mypage.aspx is not equivalent to https://mysite/myPage.aspx, well, this may be a bit too restrictive, so the good news is that with Visual Studio we can rely on the extensibility models to get the solution you need. Just replace the built in validation rule with this sample code that I have created below. You can modify this validation rule with additional logic if you desire. Add a class to your project and add the code snippet below, update the namespace if you desire, and be sure you have the using statement for the System.ComponentModel and the TestTools.WebTestingl. Make sure to rebuild the project once so the custom validation rule will show up before you try to add to the .webtest.

 

using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace TestProject5
{
  [Description("Validates that the response URL after redirects are followed is the same as the recorded response URL. QueryString parameters are ignored. The match will be done on case insensitive basis.")]
  [DisplayName("Response URL Case Insensitive")]
  public class ResponseURLCaseInsensitive : ValidationRule
  {
    public override void Validate(object sender, ValidationEventArgs e)
    {
      //determine if request is a redirect follow and validate the URL accordingly
      if (e.Request.IsRedirectFollow)
      {
        //use to upper to force case insensitive comparision
        if (e.Request.ExpectedResponseUrl.ToUpper() == e.Request.Url.ToUpper())
        {
          e.IsValid = true;
          e.Message = "Response URL successfully matched case insensitive comparison. Expected Value=" + e.Request.ExpectedResponseUrl + " , Actual Value=" + e.Request.Url;
        }
        else
        {
          e.IsValid = false;
          e.Message = "Response URL did NOT match. Expected Value=" + e.Request.ExpectedResponseUrl + " , Actual Value=" + e.Request.Url;
        }
      } //if the request did not redirect, determine if one was expected and fail if true
      else if (!string.IsNullOrEmpty(e.Request.ExpectedResponseUrl))
      {
        e.IsValid = false;
        e.Message = "The expected response redirect did not occur. Expected Response URL=" + e.Request.ExpectedResponseUrl;
      }
    }
  }
}

Comments

  • Anonymous
    October 03, 2012
    If I want to check the Query String parameters with this can it be done or does the ExpectedURL cut off the validating URL at the first Query String Parameter like it shows up in the log?

  • Anonymous
    October 29, 2015
    The code works, but the built in ResponseURL validation rule still runs post Custom Validation code and consequently the Validation fails. I expect 2 different URL depending on the state of user. I tried setting the e.Request.ExpectedResponseUrl = "1" or "2". This does not set the ExpectedResponseUrl for the WebTest and built in ResponseUrl fails the validation. Is there any way to remove the built in ResponseURL validation, or alternatively to make the test ignore this validation just for one Request in a WebTest?