다음을 통해 공유


Custom Validation Rule for Web Test

The other day someone on the Team System Testing forum posted a question about Web Test validation rules when using master pages in their ASP.Net application. The problem is that if you use a master page it will change the names of your form fields at runtime. For example if you have a form field named “LName” it will be change to something like “ctl00$ContentPlaceHolder1$LName”. So if you have created a validation rule for “LName” it will now fail as there a field with “LName” is no longer returned.

 

So how do you handle validation or extraction rules for a web test if the names of your form fields are changed at runtime by the master page. Well you can use one of the two following methods.

 

  1. Hard code the names that the master page generates. If you do this you just need to be careful that you don’t change your ASPX page in such a way that the generated name is changed.
  2. Use a custom validation rule. This allows you to write code so that you can look and make sure that “LName” is part of a field name. This way we don’t care about the generated name. A custom rule also gives you much more control over what you are validating. You can add the code for a custom validation or extraction rule to your test project or you can create them in a class library project if you want others to be able to use the rules you create.

 

Here is an example of a custom validation rule that allows you to enter a form field name and a value to check for. It then checks to see if the name you entered is in any of the field names. So if you enter “LName” as the field name to look for and you use a master pages that changes the name to “ctl00$ContentPlaceHolder1$LName” it will find this field as the code uses the String.Contains Method to check and see if “LName” is contained within the HtmlTag.

 

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using Microsoft.VisualStudio.TestTools.WebTesting;

using System.Diagnostics;

 

namespace myRules

{

    public class rule2:ValidationRule

    {

        private string _name;

        private string _value;

 

        public string Name

        {

            get {return _name;}

            set {_name = value;}

        }

 

        public string Value

        {

            get { return _value; }

            set { _value = value; }

  }

        public override string RuleName

        {

            get { return "rule2"; }

        }

        public override string RuleDescription

        {

            get

            {

                return "validate rule";

            }

        }

        public override void Validate(object sender, ValidationEventArgs e)

        {

            if (e.Response.HtmlDocument != null)

            {

                //look for input tags

                foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags(new string[] { "input" }))

                {

                    for (int i = 0; i < tag.Attributes.Count; i++)

                    {

                        string temp = tag.Attributes[i].Value.ToString();

                        //Debug.WriteLine("Name " + tag.Attributes[i].Name);

                        //Debug.WriteLine("Value " + temp);

                        //check to see if the tag contains the name we are looking for

                        if (temp.Contains(_name))

              {

                            //Debug.WriteLine("check " + tag.GetAttributeValueAsString("value"));

                            //check to see if the value of the tag name we were looking for is what we are expecting

                            if (tag.GetAttributeValueAsString("value") == _value)

                            {

                                e.IsValid = true;

                                return;

                            }

                        }

                    }

 

    }

                e.Message = "No input tag contained " + _name + " in it";

                e.IsValid = false;

            }

        }

    }

}

 

For more information on custom validation rules check out the following links

https://msdn2.microsoft.com/en-us/library/ms182556.aspx

https://blogs.msdn.com/joshch/archive/2005/11/17/494164.aspx

 

 

Enjoy and have fun.

Brian