VB Examples of Custom Extraction / Validation Rules

Can anyone provide step by step examples of setting up and writing a Custom Extraction Rule and a Custom Validation Rule in VB?

The examples provided athttp://msdn2.microsoft.com/en-us/library/ms243179.aspx andhttp://msdn2.microsoft.com/en-us/library/ms182556(VS.80).aspx are not in my language of choice.

Thank you.

[619 byte] By [LewisHorowitz] at [2007-12-21]
# 1
I will work on this. Hopefully I can post this tomorrow...
NicoleJohansonMSFT at 2007-9-10 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 2

Looks like this sample is only available in c#. However, there is a free website available (Dot Net Taxi) that converts C# code to vb, http://www.dotnettaxi.com/tools/converter.aspx. I used it on the extraction rule sample and the code came out about 95% usable. There was two problems after conversion. One problems was that vb is not case sensitive and the second problem was it did not convert the operator for comparing objects. Below is the outputed sample code after the modifications. The code compiled for me.

Imports System

Imports System.Collections.Generic

Imports Microsoft.VisualStudio.TestTools.WebTesting

Imports System.Globalization

Namespace ClassLibrary2

Public Class MyExtractionRule

Inherits ExtractionRule

Private name1 As String

Public Property Name() As String

Get

Return name1

End Get

Set(ByVal value As String)

name1 = value

End Set

End Property

Public Overloads Overrides ReadOnly Property RuleName() As String

Get

Return "MyExtractionRuleName"

End Get

End Property

Public Overloads Overrides ReadOnly Property RuleDescription() As String

Get

Return "MyExtractionRuleDescription"

End Get

End Property

Public Overloads Overrides Sub Extract(ByVal sender As Object, ByVal e As ExtractionEventArgs)

If Not e.Response.HtmlDocument Is Nothing Then

For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"input"})

If [String].Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase) Then

Dim formFieldValue As String = tag.GetAttributeValueAsString("value")

If formFieldValue = Nothing Then

formFieldValue = [String].Empty

End If

e.WebTest.Context.Add(Me.ContextParameterName, formFieldValue)

e.Success = True

Return

End If

Next

End If

e.Success = False

e.Message = [String].Format(CultureInfo.CurrentCulture, "Not Found: {0}", name)

End Sub

End Class

End Namespace

JohnnyColemanMSFT at 2007-9-10 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 3

Hello Lewis,

I am updating the documentation in the two topics you mention to include the Visual Basic code. Those changes will not be visible until the next time we release updated documentation, hopefully in August 2006. In the meantime, here is the Visual Basic code for the validation rule. The extraction rule is coming soon...

Thank you,

Nicole Johanson

Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace SampleWebTestRules

'-
' This class creates a custom validation rule named "Custom Validate Tag"
' The custom validation rule is used to check that an HTML tag with a
' particular name is found one or more times in the HTML response.
' The user of the rule can specify the HTML tag to look for, and the
' number of times that it must appear in the response.
'-
Public Class CustomValidateTag
Inherits Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule

' Specify a name for use in the user interface.
' The user sees this name in the Add Validation dialog box.
'
Public Overrides ReadOnly Property RuleName() As String
Get
Return "Custom Validate Tag"
End Get
End Property

' Specify a description for use in the user interface.
' The user sees this description in the Add Validation dialog box.
'
Public Overrides ReadOnly Property RuleDescription() As String
Get
Return "Validates that the specified tag exists on the page."
End Get
End Property

' The name of the required tag
Private RequiredTagNameValue As String
Public Property RequiredTagName() As String
Get
Return RequiredTagNameValue
End Get
Set(ByVal value As String)
RequiredTagNameValue = value
End Set
End Property

' The minimum number of times the tag must appear in the response
Private MinOccurrencesValue As Integer
Public Property MinOccurrences() As Integer
Get
Return MinOccurrencesValue
End Get
Set(ByVal value As Integer)
MinOccurrencesValue = value
End Set
End Property

' Validate is called with the test case Context and the request context.
' These allow the rule to examine both the request and the response.
'
Public Overrides Sub Validate(ByVal sender As Object, ByVal e As ValidationEventArgs)

Dim validated As Boolean = False
Dim numTagsFound As Integer = 0

For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(RequiredTagName)

Debug.Assert(String.Equals(tag.Name, RequiredTagName, StringComparison.InvariantCultureIgnoreCase))

numTagsFound += 1
If numTagsFound >= MinOccurrences Then

validated = True
Exit For
End If
Next

e.IsValid = validated

' If the validation fails, set the error text that the user sees
If Not (validated) Then
If numTagsFound > 0 Then
e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound)
Else
e.Message = String.Format("Did not find any occurences of tag '{0}'", RequiredTagName)
End If
End If
End Sub
End Class
End Namespace

NicoleJohansonMSFT at 2007-9-10 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 4

...here is the Visual Basic code for the extraction rule:

Imports System
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.WebTesting
Imports System.Globalization

Namespace ClassLibrary2

'-
' This class creates a custom extraction rule named "Custom Extract Input"
' The user of the rule specifies the name of an input field, and the
' rule attempts to extract the value of that input field.
'-
Public Class CustomExtractInput
Inherits ExtractionRule

' Specify a name for use in the user interface.
' The user sees this name in the Add Extraction dialog box.
'
Public Overrides ReadOnly Property RuleName() As String
Get
Return "Custom Extract Input"
End Get
End Property

' Specify a description for use in the user interface.
' The user sees this description in the Add Extraction dialog box.
'
Public Overrides ReadOnly Property RuleDescription() As String
Get
Return "Extracts the value from a specified input field"
End Get
End Property

' The name of the desired input field
Private NameValue As String
Public Property Name() As String
Get
Return NameValue
End Get
Set(ByVal value As String)
NameValue = value
End Set
End Property

' The Extract method. The parameter e contains the Web test context.
'
Public Overrides Sub Extract(ByVal sender As Object, ByVal e As ExtractionEventArgs)

If Not e.Response.HtmlDocument Is Nothing Then

For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"input"})

If String.Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase) Then

Dim formFieldValue As String = tag.GetAttributeValueAsString("value")
If formFieldValue Is Nothing Then

formFieldValue = String.Empty
End If

' add the extracted value to the Web test context
e.WebTest.Context.Add(Me.ContextParameterName, formFieldValue)
e.Success = True
Return
End If
Next
End If
' If the extraction fails, set the error text that the user sees
e.Success = False
e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name)
End Sub
End Class
end namespace

NicoleJohansonMSFT at 2007-9-10 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 5
Brilliant! Thanks to you for the samples and Johnny for the code converter link.
LewisHorowitz at 2007-9-10 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...

Visual Studio Team System

Site Classified