Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

ValidationRule Class

Verifies that requests return valid HTTP responses and that the contents of the response match the expected results. This class must be inherited.

Namespace: Microsoft.VisualStudio.TestTools.WebTesting
Assembly: Microsoft.VisualStudio.QualityTools.WebTestFramework (in microsoft.visualstudio.qualitytools.webtestframework.dll)

public abstract class ValidationRule
public abstract class ValidationRule
public abstract class ValidationRule

User-written validation rules must derive from this class. Validation rules are executed just after the request is complete.

Notes to Inheritors When you inherit from ValidationRule, you must override the Validate method and RuleName property.

The following code sample shows how to inherit from ValidationRule to make a rule that validates the existence of scripts on the Web page.

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

namespace MyValidationRule
    public class ValidatePageContainsScript : ValidationRule
        public override string RuleName
            get { return "Validate Script Existence"; }

        public override string RuleDescription
            get { return "Validates that the page has a script."; }

        public override void Validate(object sender, ValidationEventArgs e)
            bool validated = false;
            string foundJS ="";
            string foundVBS = "";

            string message = "Non-valid HTML document";

            if (e.Response.HtmlDocument != null)
            {   // Gets all input tags
                foreach (HtmlTag tag in e.Response.HtmlDocument
                    .GetFilteredHtmlTags(new string[] { "script" }))
                {   // Check type of script for current tag
                    if (tag.GetAttributeValueAsString("type") == "text/JavaScript")
                        foundJS = "Found JavaScript";
                    if (tag.GetAttributeValueAsString("type") == "text/VBScript")
                        foundVBS = "Found VBScript";

                if (foundVBS.Length != 0 || foundJS.Length != 0)
                    validated = true;
                    message = string.Format("{0} {1}", foundJS, foundVBS);
                   message = "No scripts in current page";
            e.IsValid = validated;
            e.Message = message;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

© 2015 Microsoft