Tuesday, May 19, 2009

Regular Expression Object RegExp Example

Below example shows Test method, IgnoreCase and Pattern property of Regular Expression object on Flight Reservation Application.

The below line in the code says that the user name should have last 5 letters as 'achin' and first letter can be anything except s and t. So xachin or wachin for example will work, but sachin and tachin will not.

'MsgBox(RegExpTest("[^st]achin", username))

Enter the below code in a new test in QTP and run it. When this example will work it will just open the Flight Reservation Application and close it.


Dim username
username = inputbox("enter username ")
MsgBox(RegExpTest("[^st]achin", username))
If (RegExpTest("[^st]achin", username))="Pass" Then
msgbox "Pass"
else
msgbox "Fail"
ExitAction
End If

SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4b.exe"
window("Title:=Login").WinEdit("AttachedText:=Agent Name:").Set username
window("Title:=Login").WinEdit("AttachedText:=Password:").Set "mercury"
window("Title:=Login").winbutton("Text:=OK").Click
window("Title:=Flight Reservation").close


Function RegExpTest(patrn, strng)
Dim regEx, retVal ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = False ' Set case sensitivity.
retVal = regEx.Test(strng) ' Execute the search test.
If retVal Then
RegExpTest = "Pass"
Else
RegExpTest = "Fail"
End If
End Function



Complete Post on RegExp object here.