Export (0) Print
Expand All

ValidationRule.Validate Method

When overridden in a derived class, this validates both the request and response.

Namespace:  Microsoft.VisualStudio.TestTools.WebTesting
Assembly:  Microsoft.VisualStudio.QualityTools.WebTestFramework (in Microsoft.VisualStudio.QualityTools.WebTestFramework.dll)

public abstract void Validate(
	Object sender,
	ValidationEventArgs e
)

Parameters

sender
Type: Object

The source of the event.

e
Type: Microsoft.VisualStudio.TestTools.WebTesting.ValidationEventArgs

A ValidationEventArgs that contains the event data.

Validate is called with the test case context and transaction context. These allow the rule to examine both the request and the response and make sure that the data matches the required values.

The following example validates whether the Web page contains any client script.

namespace TestProject1
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
    using MyValidationRule;

    public class MyCodedWebTest : WebTest
    {
        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            WebTestRequest request1 = new WebTestRequest
                ("http://localhost/MyWebSite");
            request1.ThinkTime = 13;
            ExtractHiddenFields rule1 = new ExtractHiddenFields();
            rule1.ContextParameterName = "1";
            request1.ExtractValues += new EventHandler<ExtractionEventArgs>
                (rule1.Extract);
            yield return request1;

            WebTestRequest request2 = new WebTestRequest
                ("http://localhost/MyWebSite/Default.aspx");
            request2.Method = "POST";
            FormPostHttpBody request2Body = new FormPostHttpBody();
            request2Body.FormPostParameters.Add("__VIEWSTATE",
                "{{$HIDDEN1.__VIEWSTATE}}");
            request2Body.FormPostParameters.Add("Button1", "Button");
            request2Body.FormPostParameters.Add("TextBox1",
                "Added Text in form");
            request2.Body = request2Body;
            ValidatePageContainsScript rule2 = new ValidatePageContainsScript();
            request2.ValidateResponse += new EventHandler<ValidationEventArgs>
                (rule2.Validate);
            yield return request2;
        }
    }
}

Show:
© 2015 Microsoft