QTP

 QTP Scripting Examples

 'Count Lines in a Text File

Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\gcreddy.txt")

While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
objTextstream.ReadLine
Wend
Msgbox LineCount

objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing


'Read a Text File from 4th line and perform Data Driven Testing for Login operation

Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\gcreddy.txt")

While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
myLine=objTextstream.ReadLine
If LineCount >= 4 Then
        myField=Split(myLine,",")
    SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
    Dialog("Login").Activate
    Dialog("Login").WinEdit("Agent Name:").Set myField(0)
    Dialog("Login").WinEdit("Password:").Set myField(1)
    Wait 2
    Dialog("Login").WinButton("OK").Click
    Window("Flight Reservation").Close
End If

Wend

objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing

'Read a Text File from 4th line to 6th Line and perform Data Driven Testing for Login operation

Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\gcreddy.txt")

While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
myLine=objTextstream.ReadLine
If LineCount >= 4 and LineCount<=6 Then
        myField=Split(myLine,",")
    SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
    Dialog("Login").Activate
    Dialog("Login").WinEdit("Agent Name:").Set myField(0)
    Dialog("Login").WinEdit("Password:").Set myField(1)
    Wait 2
    Dialog("Login").WinButton("OK").Click
    Window("Flight Reservation").Close
End If

Wend

objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing

'Open 1 to 10 Orders in Flight Reservation Application and export all fields to excel file (3rd sheet)
 

Dim objExcel, objWorkbook, objWorksheet

Set objExcel=CreateObject("Excel.Application")
Set objWorkbook=objExcel.Workbooks.Open("C:\Documents and Settings\Administrator\Desktop\gcreddy.xls")
Set objWorksheet=objWorkbook.Worksheets(3)
 

objWorksheet.Cells(1,1)="OrderNo"
objWorksheet.Cells(1,2)="Date of Flight"
objWorksheet.Cells(1,3)="Fly From"
objWorksheet.Cells(1,4)="Fly To"
objWorksheet.Cells(1,5)="C_Name"
objWorksheet.Cells(1,6)="Tickets"

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

objWorksheet.Cells(i+1,1)=i
    Wait 1
    objWorksheet.Cells(i+1,2)= Window("Flight Reservation").WinObject("Date of Flight:").GetROProperty("text")
    Wait 1
    objWorksheet.Cells(i+1,3) = Window("Flight Reservation").WinComboBox("Fly From:").GetROProperty("text")
    Wait 1
    objWorksheet.Cells(i+1,4)= Window("Flight Reservation").WinComboBox("Fly To:").GetROProperty("text")
    Wait 1
    objWorksheet.Cells(i+1,5) = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
    Wait 1
    objWorksheet.Cells(i+1,6) = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
   
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet=Nothing
Set objWorkbook=Nothing
Set objExcel=Nothing 


'*************************************************************************************************************
'Test Name: Verify the Total (in Flight Reservation)

'Author: abcd

'Date of Creation: 27th July 2011

'Date of Modification: NA

'Pre-requasites: Login.tsr, Orders.tsr, abcd.vbs, xyz.xml

'Test Flow :
'Launch the Application
'Enter Agent Name & Password
'Select default button
'Active Next window (FR Window)
'Open an Order
'Capture Tickets, Price and Total values
'Conver the Values from String type to Integer and Double Type
'Compare if Total=Tickets*price or not
'Form Loop Structure in order to open first 10 Orders
'Close the Application
'*************************************************************************************************************
Option Explicit
Dim Tickets, Price, Total, ord
Dim objExcel, objWorkbook, objWorksheet, i
Set objExcel=CreateObject("Excel.Application")
Set objWorkbook=objExcel.Workbooks.Open ("C:\Documents and Settings\Administrator\Desktop\qtp7.xls")
Set objWorksheet=objWorkbook.Worksheets(1)
i=1
objWorksheet.cells(i,1)="OrderNo"
objWorksheet.cells(i,2)="Tickets"
objWorksheet.cells(i,3)="Price"
objWorksheet.cells(i,4)="Total"
objWorksheet.cells(i,5)="Result"

'Login Operation (Optional)
If Not Window("Flight Reservation").Exist(3)  Then
    SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
    Dialog("Login").Activate
    Dialog("Login").WinEdit("Agent Name:").Set "asdf"
    Dialog("Login").WinEdit("Password:").SetSecure "4e2f7785badfd72026299626d52ff248a2b09b22"
    Dialog("Login").WinButton("OK").Click
End If

'Open Order
For ord=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 ord
    Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
   
    'Capturing Values
    Tickets = Window("Flight Reservation").WinEdit("Tickets:").GetVisibleText()
    Price = Window("Flight Reservation").WinEdit("Price:").GetVisibleText()
    Total = Window("Flight Reservation").WinEdit("Total:").GetROProperty("text")
    'Msgbox Tickets : Msgbox Price: Msgbox Total
   
    'Converting the Values
    Tickets = Cint(Tickets)
    Price=Cdbl (Price)
    Total =Cdbl(Total)
   
    'Comparing the Values
    i=i+1
    If Total=Tickets*Price  Then
   
    objWorksheet.cells(i,1)=ord
    objWorksheet.cells(i,2)=Tickets
    objWorksheet.cells(i,3)=Price
    objWorksheet.cells(i,4)=Total
    objWorksheet.cells(i,5)="Total is Correct"
    Else
    objWorksheet.cells(i,1)=ord
    objWorksheet.cells(i,2)=Tickets
    objWorksheet.cells(i,3)=Price
    objWorksheet.cells(i,4)=Total
    objWorksheet.cells(i,5)="Total is Inorrect"   
    End If
Next
Window("Flight Reservation").Close

objWorkbook.Save
objExcel.Quit

Set objWorksheet=Nothing
Set objWorkbook=Nothing
Set objExcel=Nothing 


'Script to close multiple instance of Application 
Dim i
i=0
While Dialog("text:=Login", "index:=i").Exist(3)
Dialog("text:=Login", "index:=i").Close
i=i+1
Wend
Msgbox "Number of opened dialogs were: "&i
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set "gcreddy"
Dialog("text:=Login").WinEdit("attached text:=Password:").Set"mercury"
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close

0 comments:

Post a Comment