Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
API Reference for Testing Tools for Visual Studio ALM
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)
Visual Basic
Public MustInherit Class ValidationRule
C#
public abstract class ValidationRule
Visual C++
public ref class ValidationRule abstract
F#
[<AbstractClass>]
type ValidationRule =  class end
JScript
public abstract class ValidationRule

The ValidationRule type exposes the following members.

  NameDescription
Protected methodValidationRuleThis class must be inherited. It cannot be instantiated.
Top
  NameDescription
Public propertyRuleDescription Obsolete. Gets the description displayed in the user interface when a rule is selected.
Public propertyRuleName Obsolete. When overridden in a derived class, gets the name displayed in the user interface when a rule is selected or shown in the editor.
Top
  NameDescription
Public methodEqualsDetermines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodValidateWhen overridden in a derived class, this validates both the request and response.
Top

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.

C#
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);
                }
                else
                {
                   message = "No scripts in current page";
                }
            }
            e.IsValid = validated;
            e.Message = message;
        }
    }
}
Visual Basic
Imports System
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace MyValidationRule
    Public Class ValidatePageContainsScript
        Inherits ValidationRule

        Public Overrides ReadOnly Property RuleName() As String
            Get
                Return "Validate Script Existence"
            End Get
        End Property

        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Validates that the page has a script"
            End Get
        End Property

        Public Overrides Sub Validate(ByVal sender As Object, _
            ByVal e As ValidationEventArgs)
            Dim validated As Boolean = False
            Dim foundJS As String = String.Empty
            Dim foundVBS As String = String.Empty
            Dim message As String = "Non-valid HTML document"

            If Not e.Response.HtmlDocument Is Nothing Then
                ' Get all input tags
                Dim tag As HtmlTag
                For Each tag In e.Response.HtmlDocument. _
                    GetFilteredHtmlTags(New String() {"script"})
                    ' Check type of script for current tag
                    If tag.GetAttributeValueAsString("type") = "text/JavaScript" _
                        Then
                        foundJS = "Found JavaScript"
                    End If
                    If tag.GetAttributeValueAsString("type") = "text/VBScript" _
                        Then
                        foundVBS = "Found VBScript"
                    End If
                Next

                If Not foundVBS.Length = 0 Or Not foundJS.Length = 0 Then
                    validated = True
                    message = String.Format("{0} {1}", foundJS, foundVBS)
                Else
                    message = "No scripts in current page."
                End If
            End If

            e.IsValid = validated
            e.Message = message

        End Sub
    End Class
End Namespace
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
RuleName and RuleDescription are obsolete.      Lolo Ninetails   |   Edit   |   Show History

Please update the sample code to reflect that RuleName and RuleDescription properties are obsolete.

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

namespace MyValidationRule
{
[DisplayName("Validate Script Existence")]
[Description("Validates that the page has a script.")]
public class ValidatePageContainsScript : ValidationRule
{
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);
}
else
{
message = "No scripts in current page";
}
}
e.IsValid = validated;
e.Message = message;
}
}
}

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker