qtp real-time script example

QuickTest Professional is an Industry leading and famous Testing Tool for Functional & Regression Testing QTP uses VB Script for scripting
We can use QTP Tool features as well as VB Script Flow Control statements for inserting verification points.
If we want to use VB Script Flow Control statements for inserting flow control statements, then we need to define Test results.
QTP has provided a facility to define Test Results, using ‘Reporter’ Utility Object to define test results.
Using Comments is a best practice in QTP Scripting to understand the code.
Creating functions for modular code is a best practice and intelligent task in scripting /Programming.
using Automation Objects is an useful task in QTP scripting
using Built-in functions and regular expressions is an useful task.
QTP Scripting has several types:
a) GUI scripting: using scripting for GUI based Applications
b) Web scripting: Automation web based applications using vb script
c) Database scripting: Using scripting for Database operations such as data comparisons and validations.
d) Excel scripting: Handling and using Excel files in Test Automation
Excel Scripting Examples-2
1) Data Driven Testing for Login Operation by fetching from an excel file
Dim objExcel, myFile, mySheet
Set objExcel=CreateObject(“Excel.Application”)
Set myFile=objExcel.Workbooks.Open(“C:Documents and SettingsgcreddyDesktopdata.xls”)
Set mySheet=myFile.Worksheets(“Sheet1″)
Rows_Count=mySheet.usedrange.rows.count
For i= 2 to Rows_Count step 1
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“text:=Login”).Activate
Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Set mySheet.Cells(i,”A”)
Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Set mySheet.Cells(i,”B”)
Wait (2)
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
Window(“text:=Flight Reservation”).Close
Next
myFile.Close
objExcel.Quit
Set objExcel=Nothing
—————————————–
2) Data Driven Testing for Login Operation by fetching Test Data from an excel file and Export Result to the Same file
Dim objExcel, myFile, mySheet
Set objExcel=CreateObject(“Excel.Application”)
Set myFile=objExcel.Workbooks.Open(“C:Documents and SettingsgcreddyDesktopdata.xls”)
Set mySheet=myFile.Worksheets(“Sheet1″)
Rows_Count=mySheet.usedrange.rows.count
For i= 2 to Rows_Count step 1
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“text:=Login”).Activate
Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Set mySheet.Cells(i,”A”)
Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Set mySheet.Cells(i,”B”)
Wait (2)
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
If Window(“text:=Flight Reservation”).Exist(12)  Then
Window(“text:=Flight Reservation”).Close
Result=”Login Operation Sucessful”
mySheet.Cells(i,”C”)=Result
Else
SystemUtil.CloseDescendentProcesses
Result=”Login Operation Failed”
mySheet.Cells(i,”C”)=Result
End If
Next
myFile.Save
myFile.Close
objExcel.Quit
Set objExcel=Nothing
——————————
3) ‘Data Driven Testing for Login Operation by fetching Test Data from an excel file
‘ Export Result and Error message to the Same file
Dim objExcel, myFile, mySheet
Set objExcel=CreateObject(“Excel.Application”)
Set myFile=objExcel.Workbooks.Open(“C:Documents and SettingsgcreddyDesktopdata.xls”)
Set mySheet=myFile.Worksheets(“Sheet1″)
Rows_Count=mySheet.usedrange.rows.count
For i= 2 to Rows_Count step 1
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“text:=Login”).Activate
Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Set mySheet.Cells(i,”A”)
Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Set mySheet.Cells(i,”B”)
Wait (2)
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
If Window(“text:=Flight Reservation”).Exist(12)  Then
Window(“text:=Flight Reservation”).Close
Result=”Login Operation Sucessful”
mySheet.Cells(i,”C”)=Result
Else
Error_Message = Dialog(“Login”).Dialog(“Flight Reservations”).Static(“Agent name must be at”).GetROProperty(“text”)
SystemUtil.CloseDescendentProcesses
Result=”Login Operation Failed”
mySheet.Cells(i,”C”)=Result
mySheet.Cells(i,”D”)=Error_Message
End If
Next
myFile.Save
myFile.Close
objExcel.Quit
Set objExcel=Nothing
——————————————————————
4) ‘Open 1 to 10 Orders in Flight Reservation window and capture Order Numbers and Customer Names
‘Export to an Excel File
Dim objExcel,myFile,mySheet,i
Set objExcel = CreateObject(“Excel.Application”)
Set myFile=objExcel.Workbooks.Open(“C:Documents and SettingsgcreddyDesktopdata.xls”)
Set mySheet = myFile.Worksheets(“Sheet2″)
row=1
mySheet.Cells(row,”A”)=”OrderNo”
mySheet.Cells(row,”B”)=”CustomerName”
If Not Window(“Flight Reservation”).Exist(3)  Then
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”,””,”C:Program FilesHPQuickTest Professionalsamplesflightapp”,”open”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set “asdf”
Dialog(“Login”).WinEdit(“Password:”).SetSecure “4d0a254623fcf8d10630f10b6ca8e776fcbc0717″
Dialog(“Login”).WinButton(“OK”).Click
End If
For  i = 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 i
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click
Customer_Name = Window(“Flight Reservation”).WinEdit(“Name:”).GetROProperty(“text”)
row=row+1
mySheet.Cells(row,”A”) = i
mySheet.Cells(row,”B”) = Customer_Name
Next
myFile.Save
myFile.Close
objExcel.Quit
Set objExcel =Nothing
—————————

 

Comments