VBScript Tutorial 8

VBScript Tutorial 8
(Excel Object Model inwards VBScript)

Excel Application operations using Excel Application Object

Excel Application Object:

It is used to perform operations on Excel Application.

Create Excel Application Object:

Set Variable = CreateObject("Excel.Application")
----------------------
Excel Application

Excel Workbook / File

Excel Worksheet / Sheet
-----------------------------------------------
Excel File Operations using VBScript Examples:
1) Create an Excel file
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True 'To persuasion the functioning (Creating Excel file) during Execution.

objExcel.Workbooks.Add 'Create New Workbook / file

objExcel.ActiveWorkbook.SaveAs "C:\Users\gcreddy\Desktop\QTP.xls" 'Save the Excel workbook /file

objExcel.Quit 'To closed the Excel Application
Set objExcel = Nothing 'To liberate the memory
---------------------------------------------------
2) Check the beingness of QTP file, if non be as well as thus exercise the file.Dim objFso, objExcel, FilePath
FilePath = "C:\Users\gcreddy\Desktop\QTP.xls"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")

If Not objFso.FileExists(FilePath) Then
objExcel.Workbooks.Add 'Create New Workbook / file
objExcel.ActiveWorkbook.SaveAs FilePath
objExcel.Quit   
End If

Set objExcel = Nothing 'To liberate the memory
----------------------------------------------------
3) Check the beingness of QTP file, if exists as well as thus opened upward the file as well as larn inwards but about data, If non be as well as thus exercise the file as well as larn inwards but about information (Using Excel Application Object only)
Dim objFso, objExcel, FilePath

FilePath = "C:\Users\gcreddy\Desktop\QTP.xlsx"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")

If Not objFso.FileExists(FilePath) Then
objExcel.Workbooks.Add
objExcel.Worksheets(1).Cells(1, 1) = "Hello UFT"
objExcel.ActiveWorkbook.SaveAs FilePath
Else
objExcel.Workbooks.Open (FilePath)
objExcel.Worksheets(1).Cells(1, 1) = "Hello UFT"
objExcel.ActiveWorkbook.Save
End If
objExcel.Quit

Set objExcel = Nothing
---------------------------------------------
Excel Objects

1) Excel Application Object
It is used to perform operations on Excel Application.

Set Variable = CreateObject("Excel.Application")
--------------------------------
2) Excel Workbook object
It is used to operate amongst specified Excel file / Workbook

Set Variable = ExcelApplicationObject.Workbooks.Add / Open("Filepath")
--------------------------------
3) Excel Worksheet object
It is used to operate amongst specified operate sheet

Set Varaible = ExcelWorkbookObject.Worksheets(Sheet Id / "Sheet name")
-------------------------------------------------------
Excel Application is ever alone one.

We may bring i or to a greater extent than Workbooks.

We may bring multiple sheets inwards every workbook.
---------------------------------------------
> Using ("Excel.Application") degree value nosotros exercise Excel Application Object.

> We exercise Excel Workbook object using Excel Application Object.

> We exercise Excel Worksheet object using Excel workbook object.
--------------------------------------
Difference betwixt File organisation object model as well as Excel object model inwards instance of Sub objects.

In File organisation object model creating Text stream object is mandatory to perform File internal operations similar Read, Write, Compare, Search etc...

In Excel Object model creating sub as well as sub-sub objects optional, if you lot desire to operate amongst multiple files as well as multiple sheets as well as thus nosotros tin role sub as well as sub-sub objects.
----------------------------------------------------------
4) Check the beingness of QTP file, if exists as well as thus opened upward the file as well as larn inwards but about data,  If non be as well as thus exercise the file as well as larn inwards but about information (Using Main as well as sub objects)

Dim objFso, objExcel, objWorkbook, objWorksheet, FilePath

FilePath = "C:\Users\gcreddy\Desktop\QTP.xlsx"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")

If Not objFso.FileExists(FilePath) Then
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Cells(1, 1) = "Hello UFT"
objWorkbook.SaveAs FilePath
Else
Set objWorkbook = objExcel.Workbooks.Open (FilePath)
Set objWorksheet = objworkbook.Worksheets(1)
objWorksheet.Cells(1, 1) = "Hello UFT"
objWorkbook.Save
End If
objExcel.Quit

Set objExcel = Nothing
------------------------------------------------
5) Read information cast Excel file as well as perform Data driven Testing for Login Functionality.
Dim objExcel, objWorkbook, objWorksheet, i, RowsCount

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

RowsCount = objWorksheet.UsedRange.Rows.Count

For i = two To RowsCount Step 1
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i, "A") 'i is Row, Influenza A virus subtype H5N1 is Column Name
Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i, 2) ' 2  is Column id
wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Next
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
------------------------------------------------
6) Read information cast Excel file as well as perform Data driven Testing for Login Functionality. And write Test Result to the Same file tertiary column.
Dim objExcel, objWorkbook, objWorksheet, i, RowsCount

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

RowsCount = objWorksheet.UsedRange.Rows.Count

For i = two To RowsCount Step 1
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i, "A") 'i is Row, Influenza A virus subtype H5N1 is Column Name
Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i, 2) ' 2  is Column id
wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist (12) Then
Window("Flight Reservation").Close
objWorksheet.Cells(i, 3) = "Login Successful - Passed"
Else
SystemUtil.CloseDescendentProcesses
objWorksheet.Cells(i, 3) = "Login Unsuccessful - Failed"
End If
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
------------------------------------------------
7) Read information cast Excel file as well as perform Data driven Testing for Login Functionality. And write Test Result as well as Error messages to the same file.Dim objExcel, objWorkbook, objWorksheet, i, RowsCount

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

RowsCount = objWorksheet.UsedRange.Rows.Count

For i = two To RowsCount Step 1
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set objWorksheet.Cells(i, "A") 'i is Row, Influenza A virus subtype H5N1 is Column Name
Dialog("Login").WinEdit("Password:").Set objWorksheet.Cells(i, 2) ' 2  is Column id
wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist (12) Then
Window("Flight Reservation").Close
objWorksheet.Cells(i, 3) = "Login Successful - Passed"
Else
objWorksheet.Cells(i, 4) =  Dialog("Login").Dialog("Flight Reservations").Static("Agent lift must hold out at").GetROProperty("text")
SystemUtil.CloseDescendentProcesses
objWorksheet.Cells(i, 3) = "Login Unsuccessful - Failed"
End If
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
--------------------------------------------
8) Read Button Names from Login Dialog as well as export to Excel file sec sheet.Dim objExcel, objWorkbook, objWorksheet, oButton, Buttons, i

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook= objExcel.Workbooks.Open ("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objworksheet = objWorkbook.Worksheets(2)

Set oButton = Description.Create
oButton("Class Name").Value = "WinButton"

Set Buttons = Dialog("Login").ChildObjects(oButton)
Msgbox Buttons.Count

objWorksheet.cells(1, 1) = "Button Names"

For i  = 0 To Buttons.Count - 1 Step 1
    objWorksheet.cells(i+2, 1) = Buttons(i).GetRoProperty("text")
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objexcel = Nothing
---------------------------------------------------
9) Read Link names from Google Home page as well as export to Excel file tertiary sheet.Dim objExcel, objWorkbook, objWorksheet, oLink, Links, i

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook= objExcel.Workbooks.Open ("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objworksheet = objWorkbook.Worksheets(3)

Set oLink = Description.Create
oLink("micclass").Value = "Link"

Set Links = Browser("Google").Page("Google").ChildObjects(oLink)
Msgbox Links.Count

objWorksheet.cells(1, 1) = "Link Names"

For i  = 0 To Links.Count - 1 Step 1
    objWorksheet.cells(i+2, 1) = Links(i).GetRoProperty("text")
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objexcel = Nothing
---------------------------------------------------
10) Read Customer names from 1 to 10 Records as well as export to Excel
Dim objExcel, objWorkbook, objWorksheet, Customer_Name, i

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook= objExcel.Workbooks.Open ("C:\Users\gcreddy\Desktop\QTP.xlsx")
Set objworksheet = objWorkbook.Worksheets(2)

objWorksheet.cells(1, 1) = "Customer Names"
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")
objWorksheet.Cells(i+1, 1) = Customer_Name
Next
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objexcel = Nothing
--------------------------------------------------
11) Create an Excel File as well as Rename 1st sail equally "Module", sec sail equally "TestCase", as well as tertiary sail equally "TestStep".
Dim objExcel
Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Add
objExcel.Worksheets(1).Name = "Module"
objExcel.Worksheets(2).Name = "TestCase"
objExcel.Worksheets(3).Name = "TestStep"

objExcel.ActiveWorkbook.SaveAs "C:\Users\gcreddy\Desktop\QTP2.xlsx"
objExcel.Quit
Set objExcel = Nothing

12) Create an Excel file as well as Add i to a greater extent than sheet.Dim objExcel
Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Add 'Create New workbook
objexcel.Worksheets.Add 'Create New worksheet

objExcel.ActiveWorkbook.SaveAs "C:\Users\gcreddy\Desktop\QTP3.xlsx"
objExcel.Quit
Set objExcel = Nothing
---------------------------------------------
Assignment:
Create an Excel file as well as Move 1st sail to tertiary position.

Creation Time:

Sheet1    Sheet2    sheet3

Move 1st sail to tertiary position

Sheet2    Sheet3    Sheet1
--------------------------------------------------
Comparison examples:

i) One to i comparing (Textual as well as binary)

ii) Many to many comparing
-------------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post