QTP Script Examples
Visit:
www.gcreddy.com
for QTP Scripts and Documents
Test Requirements
Verify Login Boundary (Check all the boundary conditions of the Login window. Checks to see if the correct message appears in the error window (Flight Reservation Message)
Verify Cancel Operation (in Login Dialog box, if user selects cancel button, before enter any data after enter data dialog box should be disappeared.)
Verify Addition, Subtraction, Multiplication and Division Operations in Calculator Application.
Verify state of Update Order Button, before open an Order and after open an Order (in Flight Reservation before opening an order Update Order button should be disabled after opening an order enabled.)
Price Consistency, In Flight Reservation (In Flight Reservation, First class price=3*Economy class price and Business class price=2*Economy class price)
Verify Total, In Flight Reservation (In Flight Reservation, Total = Tickets * Price)
Verify Flight From & Flight To Combo Boxes (In Flight reservation, select an item from Fly From: combo box and verify weather that item available or not in Fly To: combo box, like this select all items one by one in Fly From and verify weather selected items available or not in Fly To.)
Verify Order No Entry in Flight Reservation. (In Open Order dialog box, Order No object accepts numeric values only.)
Get Test Data from a Flat file and use in Data Driven Testing (through Scripting)
Get Test Data From a Database and use in Data Driven Testing (through Scripting)
Count, how many links available in Mercury Tours Home Page?
Count how many Buttons and Edit boxes available in Flight Reservation window?
13) Verify search options in Open Order Dialog box
(After selecting open order, 3 search options should be enabled and not checked,
After selecting Order No option, other options should be disabled,
After selecting Customer Name, Flight date option enabled and Order No disabled
After selecting Flight date option, Customer Name enabled and Order No disabled)
14) In Login Dialog box, Verify Help message (The message is ‘The password is 'MERCURY')
15) Count all opened Browsers on desktop and close all?
16) Create an Excel file, enter some data and save the file through VB scripting?
Solutions:
Verify Login Boundary (Check all the boundary conditions of the Login dialog box. Checks to see if the correct message appears in the error window (Flight Reservation Message)
ApplicationDir = Environment("ProductDir")
ApplicationPath = "\samples\flight\app\flight4a.exe"
If Window("Flight Reservation").Exist(2) Then
Window("Flight Reservation").Close
SystemUtil.Run ApplicationDir & ApplicationPath
Elseif Not Dialog("Login").Exist(1) Then
SystemUtil.Run ApplicationDir & ApplicationPath
End If
Dialog("Login").WinEdit("Agent Name:").Set Datatable.Value ("AgentName",dtGlobalSheet)
Dialog("Login").WinEdit("Password:").Set Datatable.Value ("Password",dtGlobalSheet)
Dialog("Login").WinButton("OK").Click
If Dialog("Login").Dialog("Flight Reservations").Exist(1) and Datatable.Value ("Status",dtGlobalSheet)="Fail" Then
Dialog("Login").Dialog("Flight Reservations").Static("Agent name must be at").Check CheckPoint("Agent name must be at least 4 characters long.")
Dialog("Login").Dialog("Flight Reservations").WinButton("OK").Click
Elseif Window("Flight Reservation").Exist(10) and Datatable.Value ("Status",dtGlobalSheet)="Pass" Then
Reporter.ReportEvent PASS,"Login: ","Succeeded"
Else
Reporter.ReportEvent Fail,"Login: ","Combination #" & Datatable.GetCurrentRow & " was not according to Excel file"
End If
Verify Cancel Operation (in Login Dialog box, if user selects cancel button, before enter any data after enter data dialog box should be disappeared.)
Invokeapplication "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinButton("Cancel").Click
If Dialog("Login").Exist (2) =True Then
Reporter.ReportEvent 1,"sd","Fail"
Else
Reporter.ReportEvent 0,"sd","Pass"
Invokeapplication "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinButton("Cancel").Click
If Dialog("Login").Exist (2) =True Then
Reporter.ReportEvent 1,"sd","Fail"
Else
Reporter.ReportEvent 0,"sd","Pass"
Invokeapplication "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4a993af45dcbd506c8451b274d2da07b38ff5531"
Dialog("Login").WinButton("Cancel").Click
If Dialog("Login").Exist (2)=True Then
Reporter.ReportEvent 1,"sd","Fail"
Else
Reporter.ReportEvent 0,"sd","Pass"
Invokeapplication "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4a993af45dcbd506c8451b274d2da07b38ff5531"
Dialog("Login").WinButton("OK").Click
Verify Addition, Subtraction, Multiplication and Division Operations in Calculator Application.
Dim aRes,sRes,dRes,mRes
VbWindow("VbWindow").Activate
VbWindow("VbWindow").VbEdit("VbEdit").Set "10"
VbWindow("VbWindow").VbEdit("VbEdit_2").Set "20"
v1=VbWindow("VbWindow").VbEdit("VbEdit").GetROProperty ("text")
v2=VbWindow("VbWindow").VbEdit("VbEdit_2").GetROProperty ("text")
VbWindow("VbWindow").VbButton("ADD").Click
aRes=VbWindow("VbWindow").VbEdit("VbEdit_3").GetVisibleText
VbWindow("VbWindow").VbButton("SUB").Click
sRes=VbWindow("VbWindow").VbEdit("VbEdit_3").GetVisibleText
VbWindow("VbWindow").VbButton("MUL").Click
mRes=VbWindow("VbWindow").VbEdit("VbEdit_3").GetVisibleText
VbWindow("VbWindow").VbButton("DIV").Click
dRes=VbWindow("VbWindow").VbEdit("VbEdit_3").GetVisibleText
v1=cdbl(v1)
v2=cdbl(v2)
aRes=cdbl (aRes)
sRes=cdbl (sRes)
mRes=cdbl (mRes)
dRes=cdbl (dRes)
If aRes=v1+v2 Then
Reporter.ReportEvent 0,"Res","Addition Passed"
else
Reporter.ReportEvent 1,"Res","Addition Failed"
End If
If sRes=v1-v2 Then
Reporter.ReportEvent 0,"Res","Subtraction Passed"
else
Reporter.ReportEvent 1,"Res","Subtraction Failed"
End If
If mRes=v1*v2 Then
Reporter.ReportEvent 0,"Res","Multiplecation Passed"
else
Reporter.ReportEvent 1,"Res","Multiplecation Failed"
End If
If dRes=v1/v2 Then
Reporter.ReportEvent 0,"Res","Division Passed"
else
Reporter.ReportEvent 1,"Res","Division Failed"
End If
4) Verify state of Update Order Button, before open an Order and after open an Order (in Flight Reservation before opening an order Update Order button should be disabled after opening an order enabled.)
Option explicit
Dim bo,ao
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "Gcreddy"
Dialog("Login").WinEdit("Password:").SetSecure "4aa8bce9984f1a15ea187a2da5b18c545abb01cf"
Dialog("Login").WinButton("OK").Click
End If
Window("Flight Reservation").Activate
bo=Window("Flight Reservation").WinButton("Update Order").GetROProperty ("Enabled")
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 "1"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
ao=Window("Flight Reservation").WinButton("Update Order").GetROProperty ("Enabled")
If bo=False Then
Reporter.ReportEvent 0,"Res","Update Order Button Disabled"
else
Reporter.ReportEvent 1,"Res","Update Order Button Enabled"
End If
If ao=True Then
Reporter.ReportEvent 0,"Res","Update Order Button Enabled"
else
Reporter.ReportEvent 1,"Res","Update Order Button Disabled"
End If
5) Price Consistency, In Flight Reservation (In Flight Reservation, First class price=3*Economy class price and Business class price=2*Economy class price)
Option explicit
Dim n,f,b,e
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4aa8b7b7c5823680cfcb24d30714c9bbf0dff1eb"
Dialog("Login").WinButton("OK").Click
End If
For n= 1 to 10 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 n
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").WinRadioButton("First").Set
f=Window("Flight Reservation").WinEdit("Price:").GetVisibleText
Window("Flight Reservation").WinRadioButton("Business").Set
b=Window("Flight Reservation").WinEdit("Price:").GetVisibleText
Window("Flight Reservation").WinRadioButton("Economy").Set
e=Window("Flight Reservation").WinEdit("Price:").GetVisibleText
f=cdbl(mid(f,2,len (f-1)))
b=cdbl(mid(b,2,len (b-1)))
e=cdbl(mid(e,2,len (e-1)))
If f=3*e and b=2*e Then
Reporter.ReportEvent 0,"Res","Pricy Consistancy is there"
else
Reporter.ReportEvent 1,"Res","Pricy Consistancy is NOT there"
End If
Window("Flight Reservation").WinButton("Button_2").Click
Window("Flight Reservation").Dialog("Flight Reservations").WinButton("No").Click
Next
6) Verify Total, In Flight Reservation (In Flight Reservation, Total = Tickets * Price)
Option Explicit
Dim t,p,tot,n
For n= 1 to 10 step 1
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "Gcreddy"
Dialog("Login").WinEdit("Password:").SetSecure "4aa892d62c529f1c23298175ad78c58f43da8e34"
Dialog("Login").WinButton("OK").Click
End If
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 n
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
t=Window("Flight Reservation").WinEdit("Tickets:").GetVisibleText
p=Window("Flight Reservation").WinEdit("Price:").GetVisibleText
tot=Window("Flight Reservation").WinEdit("Total:").GetVisibleText
t=cdbl (t)
p=Cdbl(mid(p,2,len (p-1)))
tot=Cdbl(mid(tot,2,len (tot-1)))
If tot=t*p Then
Reporter.ReportEvent 0,"Res","Calculation Passed"
else
Reporter.ReportEvent 1,"Res","Calculation Failed"
End If
Next
7) Verify Flight From & Flight To Combo Boxes (In Flight reservation, select an item from Fly From: combo box and verify weather that item available or not in Fly To: combo box, like this select all items one by one in Fly From and verify weather selected items available or not in Fly To.)
Option explicit
Dim qtp,flight_app,f,t,i,j,x,y
If Not Window("text:=Flight Reservation").Exist (7)= True Then
QTP=Environment("ProductDir")
Flight_app="\samples\flight\app\flight4a.exe"
SystemUtil.Run QTP & Flight_app
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set "asdf"
Dialog("text:=Login").WinEdit("attached text:=Password:").SetSecure "4aa5ed3daf680e7a759bee1c541939d3a54a5b65"
Dialog("text:=Login").WinButton("text:=OK").Click
End If
Window("text:=Flight Reservation").Activate
Window("text:=Flight Reservation").WinButton("window id:=6").Click
Window("text:=Flight Reservation").ActiveX("acx_name:=MaskEdBox","window id:=0").Type "090910"
f=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").GetItemsCount
For i= 0 to f-1 step 1
Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").Select (i)
x=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").GetROProperty ("text")
t=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:","x:=244","y:=147").GetItemsCount
For j= 0 to t-1 step 1
Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:","x:=244","y:=147").Select (j)
y=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:","x:=244","y:=147").GetROProperty ("text")
If x <> y Then
Reporter.ReportEvent 0,"Res","Test Passed"
Else
Reporter.ReportEvent 1,"Res","Test Failed"
End If
Next
Next
8) Verify Order No Entry in Flight Reservation. (In Open Order dialog box, Order No object accepts numeric values only.)
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4aa9ccae3bb00962b47ff7fb0ce3524c1d88cb43"
Dialog("Login").WinButton("OK").Click
End If
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 "a"
ord=Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").GetVisibleText
If ord= "a" Then
Reporter.ReportEvent 1,"Res","Order No Object is taking invalid data"
else
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set "1"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End If
9) Get Test Data from a Flat file and use in Data Driven Testing (through Scripting)
Dim fso,myfile
Set fso=createobject("scripting.filesystemobject")
Set myfile= fso.opentextfile ("F:\gcr.txt",1)
myfile.skipline
While myfile.atendofline <> True
x=myfile.readline
s=split (x, ",")
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set s(0)
Dialog("Login").WinEdit("Password:").SetSecure s(1)
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Wend
10) Get Test Data From a Database and use in Data Driven Testing (through Scripting)
Dim con,rs
Set con=createobject("Adodb.connection")
Set rs=createobject("Adodb.recordset")
con.provider=("microsoft.jet.oledb.4.0")
con.open "C:\Documents and Settings\Administrator\My Documents\Gcr.mdb"
rs.open "Select * From Login",con
While rs.eof <>True
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set rs.fields ("Agent")
Dialog("Login").WinEdit("Password:").Set rs.fields ("Password")
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
rs.movenext
Wend
11) Count, how many links available in Mercury Tours Home Page.
Set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
Set Lists = Browser("Welcome: Mercury").Page("Welcome: Mercury").ChildObjects (oDesc)
NumberOfLinks = Lists.Count()
Reporter.ReportEvent 2,"Res","Number of Links are: "&NumberOfLinks
12) Count, how many Buttons and Edit boxes available in Flight Reservation main window.
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "Gcreddy"
Dialog("Login").WinEdit("Password:").Set "mercury"
Dialog("Login").WinButton("OK").Click
End If
Set oDesc = Description.Create()
oDesc("micclass").Value = "WinButton"
Set Buttons = Window("text:=Flight Reservation").ChildObjects (oDesc)
Num_Buttons = Buttons.Count()
Set oDesc1=Description.Create()
oDesc1("micclass").Value="WinEdit"
Set Editboxes=Window("text:=Flight Reservation").ChildObjects (oDesc1)
Num_Editboxes= editboxes.count ()
sum= Num_Buttons+Num_Editboxes
Reporter.ReportEvent 2, "Res","Total Buttons: "& Num_Buttons &"Total Edit boxes: "& Num_Editboxes
13) Verify search options in Open Order Dialog box
(After selecting open order, 3 search options should be enabled and not checked,
After selecting Order No option, other options should be disabled,
After selecting Customer Name, Flight date option enabled and Order No disabled
After selecting Flight date option, Customer Name enabled and Order No disabled)
If Not window("Flight Reservation").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "Gcreddy"
Dialog("Login").WinEdit("Password:").SetSecure "4aa9ed25bc0ebde66ed726ad87d7e991347d8b9c"
Dialog("Login").WinButton("OK").Click
End If
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").Activate
oe=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").GetROProperty ("Enabled")
ce=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").GetROProperty ("Enabled")
fe=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").GetROProperty("Enabled")
oc=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").GetROProperty ("Checked")
cc=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").GetROProperty ("Checked")
fc=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").GetROProperty("Checked")
If (oe=true and ce=true and fe=true) and (oc="OFF" and cc="OFF" and fc="OFF") Then
Reporter.ReportEvent 0,"Res","Pass"
else
Reporter.ReportEvent 1,"Res","Fail"
End If
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
ono=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").GetROProperty ("Checked")
If ono="ON" Then
fd=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").GetROProperty ("Enabled")
ono=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").GetROProperty ("Enabled")
fd=false
ono=false
Reporter.ReportEvent 0,"Res","Pass"
else
Reporter.ReportEvent 1,"Res","Fail"
End If
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "OFF"
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").Set "ON"
cn=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").GetROProperty ("Checked")
If cn="ON" Then
ono=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").GetROProperty ("Enabled")
fd=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").GetROProperty ("Enabled")
fd=True
ono=false
Reporter.ReportEvent 0,"Res","Pass"
else
Reporter.ReportEvent 1,"Res","Fail"
End If
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").Set "OFF"
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").Set "ON"
fd=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").GetROProperty ("Checked")
If fd="ON" Then
ono=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").GetROProperty ("Enabled")
cn=Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Customer Name").GetROProperty ("Enabled")
cn=True
ono=false
Reporter.ReportEvent 0,"Res","Pass"
else
Reporter.ReportEvent 1,"Res","Fail"
End If
14) In Login Dialog box, Verify Help message (The message is ‘The password is 'MERCURY')
If Not Dialog("Login").Exist (2) Then
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinButton("Help").Click
message=Dialog("Login").Dialog("Flight Reservations").Static("The password is 'MERCURY'").GetROProperty("text")
If message="The password is 'MERCURY'" Then
Reporter.ReportEvent 0,"Res","Correct message "&message
else
Reporter.ReportEvent 1,"Res","Worng message "
End If
15) Count all opened Browsers on desktop and close them all?
Set oDesc = Description.Create()
oDesc("micclass").Value = "Browser"
Set Browsers =Desktop.ChildObjects (oDesc)
NumberofBrowsers = Browsers.Count()
Reporter.ReportEvent 2,"Res","Number of Browsers are: "&NumberOfBrowsers
For Counter=0 to NumberofBrowsers-1
Browsers(Counter).Close
Next
16) Create an Excel file, enter some data and save the file through VB scripting?
Dim objexcel
Set objExcel = createobject("Excel.application")
objexcel.Visible = True
objexcel.Workbooks.add
objexcel.Cells(1, 1).Value = "Testing"
objexcel.ActiveWorkbook.SaveAs("f:\exceltest.xls")
objexcel.Quit
For more QTP Information & Scripts visit: www.gcreddy.com
16
G.C.Reddy, QTP Trainer (Mobile: 9247837478)