Wednesday, February 2, 2011

small examples in qtp

1.Record and Play Back in qtp (Login and Close)
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”).WinEdit(“Agent Name:”).Set “abcd”
Dialog(“Login”).WinEdit(“Password:”).SetSecure “4c03551c45cb9c6ce024f12617d6e7f461936ba5″
Dialog(“Login”).WinButton(“OK”).Click
Window(“Flight Reservation”).Close
2. How to create excel file to store results for qtp
Set  xl= CreateObject(“Excel.application”)
xl.Visible = True
xl.Workbooks.add
a=15
b=20
If a>b  Then
xl.ActiveSheet.Cells(1,1).Value = a
Reporter.ReportEvent micDone,”a is bigger”, a
Else
xl.ActiveSheet.Cells(1,1).Value = b
Reporter.ReportEvent  micDone ,”b is bigger” , b
End If
3. How to store Store results in excel using qtp
‘ This is one of the method for importing excel in qtp
set xl=createobject(“excel.application”)
set wb=xl.workbooks.open(“D:\myexcel.xls”)
a=15
b=20
If a>b  Then
wb.ActiveSheet.Cells(1,1).Value = a
Reporter.ReportEvent micDone,”a is bigger”, a
Else
wb.ActiveSheet.Cells(1,1).Value = b
Reporter.ReportEvent  micDone ,”b is bigger” , b
End If
wb.save
wb.close
set wb=nothing
set xl=nothing
4.How to use global and Local Data Sheets in qtp
In Blank Test Case by default one action would be created.
Then write the below code in Action1–>Expert View
gd=DataTable(“Names_Global”, dtGlobalSheet)
ld=DataTable(“College”, dtLocalSheet)
msgbox gd&”_”&ld
Goto insert —>Call to new action and then it would create action2
Then write the below code in Action2–>Expert View
gd=DataTable(“Names_Global”,dtGlobalSheet)
ld=DataTable(“Number”,dtLocalSheet)
msgbox gd&”_”&ld
Put any data in Global ,Action1 and Action2 sheets and then execute.
In the above Names_Global,College and Number are the user defined coloumn names in the
corresponding sheets

‘how to create an excel using vb script
function create
set xl=createobject(“excel.application”)
xl.workbooks.add
xl.visible=true
set xl=nothing
end function
‘ read excel data using vb script
function read
set xl=createobject(“excel.application”)
set wb=xl.workbooks.open(“d:\sample.xls”)
set ws=wb.worksheets(1)
read=ws.cells(1,2).value
msgbox read
wb.close
set ws=nothing
set wb=nothing
set xl=nothing
end function
‘ write data into excel using vb script
function write
set xl=createobject(“excel.application”)
set wb=xl.workbooks.open(“d:\sample.xls”)
set ws=wb.worksheets(2)     ‘second sheet in the excel
ws.cells(6,8).value=”india”
wb.save
wb.close
set ws=nothing
set wb=nothing
set xl=nothing
end function
‘ format excel using vb script
function format
set xl=createobject(“excel.application”)
set wb=xl.workbooks.open(“d:\sample.xls”)
set ws=wb.worksheets(1)
ws.cells(4,5).value=”hai”
ws.cells(4,5).font.bold=true
ws.cells(4,5).font.colorindex=50
ws.cells(4,5).font.size=18
ws.cells(4,5).interior.colorindex=20
wb.save
wb.close
set ws=nothing
set wb=nothing
set xl=nothing
msgbox “completed”
end function
‘ function calls
create
read
write
format
1.How to get excel sheets count using vb script
n=wbobject.worksheets.count
in the above wbobject is the variable name defined by the user for the workbook object
2.How to get row and coloumn count in excel using vb script
r=sh1.usedrange.rows.count
c=sh1.usedrange.columns.count
in the above sh1 is the variable name defined by the user for worksheet object
3.How to change the cells colour (interior or outer ) and bold properties in excel using vb script
for i=1 to 4
sh1.cells(i,j).font.bold=true
sh1.cells(i,j).font.colorindex=50
sh1.cells(i,j).interior.colorindex=20
next
in the above sh1 is the variable name defined by the user for worksheet object

No comments:

Post a Comment