using System;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace MyValidationRule
{
public class ValidatePageContainsScript : <b>ValidationRule</b>
{
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;
}
}
}