Pages

Monday, September 13, 2010

Regular Expression Object RegExp

Example1

Below example shows Test method, Ignore Case 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 'rupaR' and first letter can be anything except s,w and a. So xaxrupaR or wxvrupaR for example will work, but swarupaR  and waarupaR will not.

'MsgBox(RegExpTest("[^swa]rupaR", 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("[^swa]rupaR", username))

If (RegExpTest("[^swa]rupaR", 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

No comments:

Post a Comment