Regular Expression usage within VBScript and Application Center Test (ACT) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Article Purpose
HTTP body (page) verification
URL Rewriting |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Introduction to Regular Expressions1 Test for a pattern within a string. For example, you can test whether a Web Page contains a given Credit Card number. This is called data validation. 2 Extract a substring from a string based upon a pattern match. You can find specific text within a document or input field. We will use this function to extract out Session Ids to variables (which can later be used to build the required dynamic URL Requests).
A regular expression is a pattern of text that consists of ordinary characters
(for example, letters a through z) and special characters, known as metacharacters.
The pattern describes one or more strings to match when searching a body of text.
The regular expression serves as a template for matching a character pattern to the string being searched.
The following table contains the complete list of metacharacters and their behavior in the context
of regular expressions:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constructing a Regular ExpressionNote: If I wanted to find dog followed by a period . then I would have to escape the period (which is another way of stating, do not treat the next character as a special character). The backslash \ is the escape character, so if I wanted dog. I would use dog\. In which case nothing would be returned from our target sentence and the count would be zero, as there is no dog followed by a period in the target.
The expression can become complicated, using sub-expressions and logical operators such as
AND, OR etc. The important point to note however, as a concept, is that I can check
for and extract (as the returned Match) any text that matches a given pattern. In this article only
those Regular Expressions useful to the objectives of ACT usage are explored in detail.
If you wish to get a deeper understanding of Regular Expressions there are many references
on the Web (Google search Regular Expression).
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Regular Expression usage in VBscriptThe Properties of the VBscript object are:- * Pattern - A string that is used to define the regular expression. This must be set before use of the regular expression object. Patterns are described in more detail below. * IgnoreCase - A Boolean property that indicates if the match should ignore case (i.e. Case insensitive) By default, IgnoreCase is set to False. * Global - A Boolean property that indicates if the regular expression should be tested against all possible matches in a string. By default, Global is set to False. The Methods of the VBscript object are:- * Test (string) - The Test method takes a string as its argument and returns True if the regular expression can successfully be matched against the string, otherwise False is returned. * Replace (search-string, replace-string) - The Replace method takes 2 strings as its arguments. If it is able to successfully match the regular expression in the search-string, then it replaces that match with the replace-string, and the new string is returned. If no matches were found, then the original search-string is returned. * Execute (search-string) - The Execute method works like Replace, except that it returns a Matches collection object, containing a Match object for each successful match. It doesn't modify the original string. What is interesting, at least for using the VBscript RegEx object in ACT, is the Matches collection that is returned after the Execute method have been invoked. Using this Object we can parse out substrings and place them into variables for future use (Session Ids etc.) An example using the properties and Matches\SubMatches is now given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
An example VBscript Regular Expression, using the Matches objectmydata = "14:10:48.019591 IP MyServer.2912 > MyClient.1494: P 1659928455:1659928474(19) ack 2614425189 win 16456 (DF)" Dim oRegExp Set oRegExp = New RegExp oRegExp.Global = True oRegExp.IgnoreCase = True oRegExp.Pattern = "(: P |: R |: F |: FP )(\d.*)(:)(\d.*)(\(\d)(.*)" Set colMatches = oRegExp.Execute(mydata) if colMatches.Count > 0 Then s1 = colMatches(0).SubMatches(1) s3 = colMatches(0).SubMatches(3) End If wscript.echo(s1) wscript.echo(s3) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Regular Expression (VBscript) usage in ACT
In order to check for the existence of a given piece of text, the following code is called, which includes a Regular Expression. Function CheckBody(sTarget) Dim oRegExp Dim oMatches Dim sBody Dim bFoundTarget bFoundTarget = False sBody = g_oResponse.Body Set oRegExp = New RegExp oRegExp.Pattern = sTarget ' run the search Set oMatches = oRegExp.Execute(sBody) If oMatches.Count > 0 Then bFoundTarget = True Else ActTrace L_ErrPageNotFound_Text & " " & sTarget End If CheckBody = bFoundTarget End Function This sample code is taken directly from the Duwamish 7.0 Sample in the ACTSamples (zip) that comes with ACT. The string to be searched for is passed to this function in the sTarget variable. the g_oResponse.Body is a global variable which contains the HTTP response which is to be searched. For this simple expression only the number of matches returned by the Execute Method is needed, this is expected to be greater than zero. In the sample if the text is not found an error is logged to the trace but the test continues, in practice you may want to terminate the test or take other action. The above sub routine is invoked using the code segment:- Set g_oResponse = g_oConnection.Send(g_oRequest) If (g_oResponse Is Nothing) Then ActTrace L_ErrRequest_Text Else If IsSuccessful(g_oResponse) Then Call SetViewState(g_oResponse) CheckBody(sPageString) End If End If What this code is doing is retrieving a page on an Open connection then retrieving the _ViewState variable (this is explained next for the URL Rewriting example) then validating the correct page has been retrieved by calling the CheckBody Sub routine. The Duwamish 7.0 Sample in the ACTSamples has many useful structures (extensive use of Sub routines and constants) and is only repeated here to reinforce the Regular expression usage.
The following example is also taken from the Duwamish 7.0 Sample in the ACTSamples, however, the code here has been changed to implement the same functionality using Regular Expressions. In the Duwamish 7.0 Sample the __VIEWSTATE is extracted using the InStr function which returns the Position of a given string, which can then be used to extract a string using the Mid function. Using Regular Expressions for this URL Rewriting offers greater flexibility over the InStr usage as the variable to be extracted is often delimited by other variables which can only be referenced using pattern matching. The original code from the Duwamish 7.0 Sample is commented out, to show the original implementation. Sub SetViewState(g_oResponse) Dim Pos, PosStart, PosEnd Dim res, vState Dim oRegExp, colMatches Set oRegExp = New RegExp oRegExp.Global = True oRegExp.IgnoreCase = True oRegExp.Pattern = "(__VIEWSTATE\"" value=\"")(.*)(\"")" If (g_oResponse Is Nothing) Then ActTrace L_ErrRequest_Text Else Set colMatches = oRegExp.Execute(g_oResponse.Body) If colMatches.Count > 0 Then res = colMatches(0).SubMatches(1) 'COMMENT Pos = InStr(g_oResponse.Body, "__VIEWSTATE") 'COMMENT If Pos > 0 Then 'COMMENT PosStart = InStr(Pos, g_oResponse.Body, "value=""") 'COMMENT PosStart = PosStart + Len ("value=""") 'COMMENT PosEnd = InStr(PosStart, g_oResponse.Body, """") 'COMMENT res = Mid(g_oResponse.Body, PosStart, PosEnd - PosStart) Test.SetGlobalVariable "vState",res g_ViewState = Test.GetGlobalVariable("vState") End If End If End Sub The actual g_oResponse.Body contains the following line:- input type="hidden" name="__VIEWSTATE" value="dDwtMTQ2NjQ3MjkwNjt0PHkD3PMCY" In this example the SubMatches collection is used to get to the variable portion. The g_ViewState is a global variable which is later used in the URL Request, which is constructed using:- g_oRequest.Verb = "POST" g_oRequest.Body = "__VIEWSTATE=" & g_ViewState & "&LogonEmailTextBox=" & oUser.Name & _ "&LogonPasswordTextBox=" & oUser.Password & "&LogonButton=Logon" Set g_oResponse = g_oConnection.Send(g_oRequest) Notice here the oUser.Name and oUser.Password variables are taken from the Users Table to give the variable parameters which is the subject of a different article. The important variable to note here is the g_ViewState which was previously parsed out (using Regular Expressions) from the last HTTP Response. Using the flexibility of Regular Expressions ANY variable can be parsed out for URL Rewriting, although this example (the __VIEWSTATE) is a simple case. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Conclusions
Application Center Test provides a framework (Objects) that allows for a Windows Scripting Host (WSH) language, such as VBScript,
to manipulate and control HTTP streams.
|
No guarantee (or claim) is made regarding the accuracy of this information. Any questions or comments should be sent to:-