QTP Excel Script

HP UFT (Formerly QTP) Excel Script


 Data Driven Testing for Login Functionality (In Flight Reservation Application), by Reading Test Data from an external Excel File and exporting, Test Result to the same File.

Note: In this Test / Test Script I used maximum formalities, writing comments, Error prevention, Verification Point, Error Handling and Comments.

‘**********************************************************’ Test ‘Test Name: Data Driven Testing for Login Operation

‘Author: G C Reddy

‘Date of Creation: 20th May 2013

‘Date of Modification: NA

‘Pre-setup: None

‘Pre-requisites: Login.tsr, Excel Application Objects, input.xls(Test data file), xyz (module1.vbs Library file)

‘Test Flow
‘i) Create Excel Application Objects in order to read and connect Test data to the Test.
‘ii) Define Column Header (If Required)
‘iii)Generate the Login Functionality
a) Launch the Application,
    b) Enter Agent Name 
    c) Enter password, 
    c) Click on default button
‘iv) Insert Verification point and return Test Result and error message to the same file
‘v) Provide Error handling in the Else part
‘vi) Form Loop structure to perform multiple iterations
‘7) Connect Test Data using WorkSheet Object
‘8) Save the WorkBook, Close Excel Application and release the memory for all three Objects
Option Explicit ‘It forces declaration of all variables in a script, so that we can avoid miss spell problems.
Dim objExcel, objWorkbook, objWorksheet, recordCount
Set objExcel = createObject(“Excel.Application”)
Set objWorkbook = objExcel.Workbooks.Open(“C:Documents and SettingsAdministratorDesktopinput.xls”)
Set objWorksheet = objWorkbook.Worksheets(1)

objworksheet.cells(1, 3) = “Test result” ‘Define Column
objworksheet.cells(1, 4) = “Error message”

recordcount = objWorksheet.usedrange.rows.count

For i = 2 To recordcount Step 1
If Not Dialog(“Login”).Exist(3) Then
SystemUtil.Run “C:Program FilesHPUnified Functional Testingsamplesflightappflight4a.exe”,””,”C:Program FilesHPUnified Functional Testingsamplesflightapp”,”open”
End if
Dialog(“Login”).Activate @@ hightlight id_;_264984_;_script infofile_;_ZIP::ssf1.xml_;_
Dialog(“Login”).WinEdit(“Agent Name:”).Set objWorksheet.cells(i, 1) @@ hightlight id_;_133936_;_script infofile_;_ZIP::ssf2.xml_;_
Dialog(“Login”).WinEdit(“Password:”).Set objWorksheet.cells(i, 2)
Dialog(“Login”).WinButton(“OK”).Click @@ hightlight id_;_133938_;_script infofile_;_ZIP::ssf4.xml_;_

If Window(“Flight Reservation”).exist(12) Then
Window(“Flight Reservation”).Close
objWorksheet.Cells(i, 3) = “Login Successful -Passed”

If Dialog(“Login”).Dialog(“Flight Reservations”).Exist(3) Then
objWorksheet.Cells(i, 4) = Dialog(“Login”).Dialog(“Flight Reservations”).Static(“Agent name must be at”).GetROProperty(“text”)
Dialog(“Login”).Dialog(“Flight Reservations”).WinButton(“OK”).Click
objWorksheet.Cells(i, 3) = “Login UnSuccessful -Failed”
End If
End If

Set objWorksheet = Nothing ‘To release the memory immediately after execution
Set objWorkbook = Nothing
Set objExcel = Nothing

Note: above 3 three statements are optional statements, if we don’ use those statements them memory will be released after re launching the Tool.

QTP Test / Test Script Guidelines:

Use Comments in order to increase the readability

Don’t use extra spaces in order to increase the test execution performance.

Provide Error handling statements wherever is necessary.

Whenever you use Logical statements (Flow Control Statements – Conditional and Loop)   try to use one blank line, that Coding standard not a Syntax rule.