'Handling dynamic Objects
Ex1:
For i = 1 to 4 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").WinMenu("Menu").Select "File;Fax Order..."
Window("Flight Reservation").Dialog("text:=Fax Order No. [0-9]").Activate
Window("Flight Reservation").Dialog("text:=Fax Order No. [0-9]").ActiveX("acx_name:=MaskEdBox").Type "11111111111"
Window("Flight Reservation").Dialog("text:=Fax Order No. [0-9]").WinButton("text:=&Send").Click
Next
---------------------------------------------------------------------------------------------
Ex2:
For i = 1 to 4 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").WinMenu("Menu").Select "File;Fax Order..."
Window("Flight Reservation").Dialog("text:=Fax Order No. "&i).Activate
Window("Flight Reservation").Dialog("text:=Fax Order No. "&i).ActiveX("acx_name:=MaskEdBox").Type "11111111111"
Window("Flight Reservation").Dialog("text:=Fax Order No. "&i).WinButton("text:=&Send").Click
Next
---------------------------------------------------------------------------------------------
Regular Expression using QTP Tool Options:
---------------------------------------------------------------------
'Count How many times the Word "QTP" appeared in a Text file
Dim objFso, objTextstream, objRegEx, Matches , myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\sample.txt")
myContent = objTextstream.ReadAll
Set objRegEx = New RegExp
objRegEx.Pattern = "qtp"
objRegEx.Global = True 'To find all matches
objRegEx.IgnoreCase = True 'To ignore cases
Set Matches = objRegEx.Execute(myContent)
Msgbox Matches.Count
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
Set objRegEx = Nothing
Dim objFso, objTextstream, objRegEx, Matches , myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\sample.txt")
myContent = objTextstream.ReadAll
Set objRegEx = New RegExp
objRegEx.Pattern = "qtp"
objRegEx.Global = True 'To find all matches
objRegEx.IgnoreCase = True 'To ignore cases
Set Matches = objRegEx.Execute(myContent)
Msgbox Matches.Count
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
Set objRegEx = Nothing
Regular Expression using RegExp Object in VBScript
VBScript provides a RegExp object to handle regular expressions.This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings.
Properties of RegExp Object
Following are the properties of regexp object:
IgnoreCase: By default regular expression is case sensitive, to make it case insensitive, set value as “True”
Pattern: We can define the pattern of regular expression in the pattern property. Pattern can include literal and meta characters like .,*,/d as discussed in the earlier post on how to create regular expressions in QTP
Global: To return or replace all matches, set Global as True. If set as False, finds the first match only
‘’Defining a regular expression
Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"
Methods of RegExp object
Execute method: executes a match against the specified string. Returns a Matches
collection, which contains a Match object for each match. The Matchobject can also contain SubMatches collection.
Execute method has following properties:
o Item
§ Item.value – value of item in the collection
§ Firstindex – First instance of match location
o Count – Count of instances matching the regular expression
·Replace method: replaces the part of the string found in a match with another string. Does replace the pattern defined for object with the replace value.
Syntax: regExpobj.replace(teststring,”newpattern”)
Test method: executes an attempted match and returns True or False based on match found.
Sample Code explaining properties and methods of RegExp Object.
Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"
testString = "this ant is an insect"
Set colmatch = newRegExp.Execute(teststring)
For each match in colmatch
Msgbox match.Value
msgbox colmatch.count
Next
boolMatch = newRegExp.Test(teststring)
msgbox boolMatch
newRegExp.pattern ="a"
strString = newRegExp.replace(teststring,"b")
msgbox strstring
No comments
Post a Comment