Your SlideShare is downloading. ×
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Excel Scripting
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Excel Scripting

3,046

Published on

QTP Excel Scripting Examples; …

QTP Excel Scripting Examples;
'Objects in Excel Object Model
a) Excel Application - Excel Application Object
b Excel Workbook / File - Workbook Object
c) Excel Worksheet / sheet - Worksheet
Important Operations on Excel files for Test Automation Using QuickTest Professional (QTP)
a) Create Excel Files
b) Open Excel Files
c) Copy Excel Files
d) Delete Excel Files
e) Move Excel Files
f) Read Data
e) Read Data for Data driven Testing
f) Write Data
g) Write Test Result
h) Comparing data (One to one)
i) Comparing data (One to Many)
j) Comparing data (Many to one)
k) Comparing data (Many to Many Exact)
l) Comparing data (Many to Many Textual)
m) Searching for strings

Published in: Technology
3 Comments
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,046
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
3
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. www.gcreddy.com Visit: www.gcreddy.com for QTP Information Excel Scripting in QTPExcel File / Work Book Operations--------------------------------------------------Objects in Excel Object Modela) Excel Application - Excel Application Objectb) Excel Workbook / File - Workbook Objectc) Excel Worksheet / sheet - Worksheet------------------------------------------------------Note: Without creating Work Book Object and Work Sheet Object,we can perform all Excel Application Operations using ExcelApplication(Main) Object, but for user friendliness we use thoseobjects.Creating Excel Application ObjectSet Variable=CreateObject("Excel.Application")Create ExcelApplication ObjectDim objExcelSet objExcel=CreateObject("Excel.Application")Important Operations on Excel files for Test Automation Using QuickTestProfessional (QTP)a) Create Excel Filesb) Open Excel Files www.gcreddy.com 1
  • 2. www.gcreddy.comc) Copy Excel Filesd) Delete Excel Filese) Move Excel Filesf) Read Datae) Read Data for Data driven Testingf) Write Datag) Write Test Resulth) Comparing data (One to one)i) Comparing data (One to Many)j) Comparing data (Many to one)k) Comparing data (Many to Many Exact)l) Comparing data (Many to Many Textual)m) Searching for strings Examples:---------------------------------------------1) Create Excel file /Work bookDim objExcelSet objExcel=CreateObject("Excel.Application")objExcel.Visible=True To view the OperationsobjExcel.Workbooks.Add Creatining Excel file / workbookobjExcel.ActiveWorkbook.SaveAs "C:Documents andSettingsAdministratorDesktopgcreddy.xls"objExcel.Quit To Quit the Excel ApplicationSet objExcel=Nothing----------------------------------------------------------- www.gcreddy.com 2
  • 3. www.gcreddy.com2) Check the existence of the File If exists then open the file andenter some data If Not exists Create the Excel file /Work book and enter some dataDim objExcel, objFso, FilePathFilePath="C:Documents andSettingsAdministratorDesktopgcreddy.xls"Set objFso=CreateObject("Scripting.FileSystemObject")Set objExcel=CreateObject("Excel.Application")If objFso.FileExists(FilePath) Then objExcel.Workbooks.Open (FilePath) objExcel.Worksheets("Sheet1").Cells(1,1)="VB Script" objExcel.ActiveWorkbook.Save Else objExcel.Workbooks.Add objExcel.ActiveSheet.Cells(2,2)="VB Script" objExcel.ActiveWorkbook.SaveAs (Filepath)End IfobjExcel.Quit To Quit the Excel ApplictionSet objExcel=Nothing------------------------------------------------------3) Fetch Test Data directly from an Excel file and perform Datadriven testing for Login OperationDim objExcel, objWorkbook, objWorksheetCreate Excel application Object that can be used to perform operationson Excel ApplictionSet objExcel=CreateObject("Excel.Application")Create WorkBook Object using Excel application Object that can be usedto perform operations on Excel Work BooksSet objWorkbook=objExcel.Workbooks.Open ("C:Documents andSettingsAdministratorDesktopinput.xls")Create Work sheet object Using Work Book Object, that can be used toperform operations on Excel SheetsSet objWorksheet=objWorkbook.Worksheets("Sheet1")Rows_Count=objWorksheet.usedrange.rows.countFor i= 2 to Rows_Count Step 1SystemUtil.Run "C:Program FilesHPQuickTestProfessionalsamplesflightappflight4a.exe","","C:ProgramFilesHPQuickTest Professionalsamplesflightapp","open" www.gcreddy.com 3
  • 4. www.gcreddy.comDialog("Login").ActivateDialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i,"A")Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i,"B")Wait 1Dialog("Login").WinButton("OK").ClickWindow("Flight Reservation").CloseNextobjExcel.QuitSet objWorksheet=NothingSet objWorkbook=NothingSet objExcel=Nothing-------------------------------------------------------------------------4) Fetch Test Data directly from an Excel file and perform Datadriven testing for Login OperationExport Test Results to the same fileDim objExcel, objWorkbook, objWorksheetCreate Excel application Object that can be used to perform operationson Excel ApplictionSet objExcel=CreateObject("Excel.Application")Create WorkBook Object using Excel application Object that can be usedto perform operations on Excel Work BooksSet objWorkbook=objExcel.Workbooks.Open ("C:Documents andSettingsAdministratorDesktopinput.xls")Create Work sheet object Using Work Book Object , that can be used toperform operations on Excel SheetsSet objWorksheet=objWorkbook.Worksheets("Sheet1")objWorksheet.Cells(1,3)="Results"Rows_Count=objWorksheet.usedrange.rows.countFor i= 2 to Rows_Count Step 1SystemUtil.Run "C:Program FilesHPQuickTestProfessionalsamplesflightappflight4a.exe","","C:ProgramFilesHPQuickTest Professionalsamplesflightapp","open"Dialog("Login").ActivateDialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i,"A")Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i,"B")Wait 1Dialog("Login").WinButton("OK").ClickIf Window("Flight Reservation").Exist(12) Then Window("Flight Reservation").Close objWorksheet.Cells(i,"C")="Login Successful" www.gcreddy.com 4
  • 5. www.gcreddy.com ElseSystemUtil.CloseDescendentProcessesobjWorksheet.Cells(i,"C")="Login Filed"End IfNextobjWorkbook.SaveobjExcel.QuitSet objWorksheet=NothingSet objWorkbook=NothingSet objExcel=Nothing5) Fetch Test Data directly from an Excel file and perform Datadriven testing for Login OperationExport Test Results & Error Messgae to the same fileDim objExcel, objWorkbook, objWorksheet, rows_CountSet objExcel=CreateObject("Excel.Application")Set objWorkbook=objExcel.Workbooks.Open ("C:Documents andSettingsAdministratorDesktopinput.xls")Set objWorksheet=objWorkbook.Worksheets(1)objWorksheet.Cells(1,3)="Test Result"objWorksheet.Cells(1,4)="Error Message"rows_Count=objWorksheet.usedrange.rows.countFor i= 2 to rows_Count Step 1 SystemUtil.Run "C:Program FilesHPQuickTestProfessionalsamplesflightappflight4a.exe","","C:ProgramFilesHPQuickTest Professionalsamplesflightapp","open" Dialog("Login").Activate Dialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i,1) Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i, "B") Dialog("Login").WinButton("OK").ClickIf Window("Flight Reservation").Exist(12) Then Window("Flight Reservation").CloseobjWorksheet.Cells(i, 3)="Login Successful"ElseobjWorksheet.Cells(i, 3)="Login Failed" www.gcreddy.com 5
  • 6. www.gcreddy.comobjWorksheet.Cells(i, 4)=Dialog("Login").Dialog("FlightReservations").Static("Agent name must be at").GetROProperty ("text")SystemUtil.CloseDescendentProcessesEnd IfNextobjWorkbook.SaveobjExcel.QuitSet objWorksheet=NothingSet objWorkbook=NothingSet objExcel=Nothing6)Using While...Wend Loop------------------------Dim objExcel, objWorkbook, objWorksheet, rows_Count, iSet objExcel=CreateObject("Excel.Application")Set objWorkbook=objExcel.Workbooks.Open ("C:Documents andSettingsAdministratorDesktopinput.xls")Set objWorksheet=objWorkbook.Worksheets(1)objWorksheet.Cells(1,3)="Test Result"objWorksheet.Cells(1,4)="Error Message"rows_Count=objWorksheet.usedrange.rows.count i= 2While i<= rows_Count SystemUtil.Run "C:Program FilesHPQuickTestProfessionalsamplesflightappflight4a.exe","","C:ProgramFilesHPQuickTest Professionalsamplesflightapp","open" Dialog("Login").Activate Dialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i,1) Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i, "B") Dialog("Login").WinButton("OK").ClickIf Window("Flight Reservation").Exist(12) Then Window("Flight Reservation").CloseobjWorksheet.Cells(i, 3)="Login Successful"ElseobjWorksheet.Cells(i, 3)="Login Failed"objWorksheet.Cells(i, 4)=Dialog("Login").Dialog("FlightReservations").Static("Agent name must be at").GetROProperty ("text")SystemUtil.CloseDescendentProcesses www.gcreddy.com 6
  • 7. www.gcreddy.comEnd Ifi=i+1WendobjWorkbook.SaveobjExcel.QuitSet objWorksheet=NothingSet objWorkbook=NothingSet objExcel=Nothing7) Capture Link names from Google home page and export toExcel file 3rd sheetDim ObjExcel,ObjWorkbook,ObjWorksheetDim oLink,Links,myLink,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(3) ObjWorksheet.Cells(1,1)="Link Names"Set oLink=Description.CreateoLink("micclass").value="Link"SetLinks=Browser("title:=Google").Page("title:=Google").ChildObjects(oLink)For i=0 to Links.Count-1 step 1 myLink=Links(i).GetRoProperty("text") ObjWorksheet.Cells(i+2,1)=myLinkNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=Nothing www.gcreddy.com 7
  • 8. www.gcreddy.comSet ObjExcel=Nothing----------------------------------------------------------------------------------8) Capture Button names from Login Dialog (Flight ReservationApplication) and export to Excel file 3rd sheetDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,1)="Button Names"Set oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2,1)=myButtonNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing-----------------------------------------------------------------------------------9) Read/capture order numbers and customer names from 1 - 10orders in Flight Reservation window and export to excel file 2nd sheetDim objExcel, objWorkBook, objWorkSheet, ord, C_NameSet objExcel = createobject("Excel.Application")Set objWorkBook = objExcel.Workbooks.Open("C:Documents andSettingsgcrDesktopSample.xls")Set objWorkSheet = objWorkBook.Worksheets(2)objWorkSheet.cells(1,1) = "Order No."objWorkSheet.cells(1,2) = "C-Name" www.gcreddy.com 8
  • 9. www.gcreddy.comFor ord= 1 to 10 Step 1 Window("Flight Reservation").Activate Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").Dialog("OpenOrder").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("OpenOrder").WinEdit("Edit").Set ord Window("Flight Reservation").Dialog("OpenOrder").WinButton("OK").Click Wait 1 C_Name = Window("FlightReservation").WinEdit("Name:").GetROProperty("text")objWorkSheet.cells(ord+1,1) = ordobjWorkSheet.cells(ord+1,2) =C_NameNextobjWorkBook.SaveobjExcel.QuitSet objWorkSheet=NothingSet objWorkBook=NothingSet objExcel=Nothing10) One to One Comparison and Exact match----------------------------------------Capture Button names from Login Dialog (Flight ReservationApplication) and Perform One to One Comparison and Exact matchDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,2)="Buttons"Set oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2, 2)=myButton www.gcreddy.com 9
  • 10. www.gcreddy.comNextrows_Count= ObjWorksheet.usedrange.rows.countFor j= 2 to rows_Count step 1Expected=ObjWorksheet.Cells(j, 1)Actual=ObjWorksheet.Cells(j, 2)If Expected=Actual ThenObjWorksheet.Cells(j, 3)="Pass"ElseObjWorksheet.Cells(j, 3)="Fail"End IfNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing11) One to One Textual Comparison------------------------------Capture Button names from Login Dialog (Flight ReservationApplication) and Perform and Perform One to One Textual ComparisonDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,2)="Buttons"Set oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2, 2)=myButtonNextrows_Count= ObjWorksheet.usedrange.rows.countFor j= 2 to rows_Count step 1 www.gcreddy.com 10
  • 11. www.gcreddy.comExpected=ObjWorksheet.Cells(j, 1)Actual=ObjWorksheet.Cells(j, 2)If StrComp (Expected,Actual,1)=0 ThenObjWorksheet.Cells(j, 3)="Pass"ElseObjWorksheet.Cells(j, 3)="Fail"End IfNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing--------------------------------------------------------------------------12) Many to Many Comparison-----------------------------------Capture Button names from Login Dialog (Flight ReservationApplication) and Perform and Perform Many to Many ComparisonDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,2)="Buttons"Set oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2, 2)=myButtonNextrows_Count= ObjWorksheet.usedrange.rows.countFor j= 2 to rows_Count step 1Expected=ObjWorksheet.Cells(j, 1) www.gcreddy.com 11
  • 12. www.gcreddy.comFor k=2 to rows_Count step 1 Actual=ObjWorksheet.Cells(k, 2) If Expected=Actual Then Flag =1 Exit for else Flag= 0 End IfnextIf Flag=1 ThenObjWorksheet.Cells(j, 3)="Pass"ElseObjWorksheet.Cells(j, 3)="Fail"End IfNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing-------------------------------------------------------------------13) Many to Many Textual ComparisonCapture Button names from Login Dialog (Flight ReservationApplication) and Perform and Perform Many to Many TextualComparison-----------------------------------Capture Button names from Google home page and export to Excel file3rd sheetDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,2)="Buttons" www.gcreddy.com 12
  • 13. www.gcreddy.comSet oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2, 2)=myButtonNextrows_Count= ObjWorksheet.usedrange.rows.countFor j= 2 to rows_Count step 1Expected=ObjWorksheet.Cells(j, 1)For k=2 to rows_Count step 1 Actual=ObjWorksheet.Cells(k, 2) If StrComp (Expected,Actual,1)= 0 Then Flag =1 Exit for else Flag= 0 End IfnextIf Flag=1 ThenObjWorksheet.Cells(j, 3)="Pass"ElseObjWorksheet.Cells(j, 3)="Fail"End IfNextObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing---------------------------------------------------------------------------------------14) Create Excel file and Rename 1st sheet as "Module", 2ndSheet as "Test Case", and 3rd Sheet as "Test Step"Dim objExcelSet objExcel=CreateObject("Excel.Application") www.gcreddy.com 13
  • 14. www.gcreddy.comobjExcel.Visible=TrueobjExcel.Workbooks.AddobjExcel.Worksheets("Sheet1").Name="Module"Wait 4objExcel.Worksheets("Sheet2").Name="TestCase"Wait 4objExcel.Worksheets("Sheet3").Name="TestStep"objExcel.ActiveWorkbook.SaveAs "C:Documents andSettingsAdministratorDesktopabcd.xls"objExcel.QuitSet objExcel=Nothing-------------------------------------------------------------------------15) Create an Excel file and add one moreDim objExcelSet objExcel=CreateObject("Excel.Application")objExcel.Visible=TrueobjExcel.Workbooks.Add Creating Work BookobjExcel.Worksheets.Add Creating Work SheetWait 4objExcel.ActiveWorkbook.SaveAs "C:Documents andSettingsAdministratorDesktopabcde.xls"objExcel.QuitSet objExcel=Nothing--------------------------------------------------------------------------------------15) Capture Button names from Login Dialog (Flight ReservationApplication) and perform Many to Many Complete ComparisonCapture Button names from Google home page and export to Excel file3rd sheetDim ObjExcel,ObjWorkbook,ObjWorksheetDim oButton,Buttons,myButton,iSet ObjExcel=CreateObject("Excel.Application")Set ObjWorkbook=ObjExcel.Workbooks.Open("C:Documents andSettingsAdministratorDesktopinput.xls")Set ObjWorksheet=ObjWorkbook.Worksheets(2) ObjWorksheet.Cells(1,2)="Buttons" www.gcreddy.com 14
  • 15. www.gcreddy.comSet oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)For i=0 to Buttons.Count-1 step 1 myButton=Buttons(i).GetRoProperty("text") ObjWorksheet.Cells(i+2, 2)=myButtonNextrows_Count= ObjWorksheet.usedrange.rows.countx =0For j= 2 to rows_Count step 1 Expected=ObjWorksheet.Cells(j, 1) flag = 0 For k=2 to rows_Count step 1 Actual=ObjWorksheet.Cells(k, 2) If StrComp (Expected,Actual,1)= 0 Then Flag =1 End If x=x+1 increment the comparison count next If Flag=1 Then ObjWorksheet.Cells(j, 3)="Pass" Else ObjWorksheet.Cells(j, 3)="Fail" End If msgbox x inner loop comparison valuesNext msgbox x Total number of comparisonsObjWorkbook.SaveObjExcel.QuitSet ObjWorksheet=NothingSet ObjWorkbook=NothingSet ObjExcel=Nothing www.gcreddy.com 15

×