web list methods
TRANSCRIPT
![Page 1: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/1.jpg)
WEB LIST Methods
Find All List Objects on a Web Page
Sub ChildObjects_Example()
'The following example uses the ChildObjects method to find all the
'list objects on a Web page, and then to select an item in each list.
Set oDesc = Description.Create()
oDesc("micclass").Value = "WebList"
Set Lists = Browser("Mercury Interactive").Page("Mercury Interactive").ChildObjects(oDesc)
NumberOfLists = Lists.Count()
For i = 0 To NumberOfLists - 1
Lists(i).Select i + 1
Next
End Sub
Find All WebEdit Objects on a Web Page and Set a Value for a Specific One
Sub ChildObjects_Example()
'The following example retrieves the collection of
'WebEdit objects in order to find a specific
'WebEdit object in a page and set a value for it.
Dim EditToSearch, ValueToSet, NumberOfEdits
'This is the value of the 'name' property for the WebEdit object we want to find.
EditToSearch = "Customer Name"
ValueToSet = "6549865461"
1
![Page 2: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/2.jpg)
'Create a description object to help retrieve all WebEdit objects in a specific page.
Set oDesc = Description.Create()
oDesc("micclass").Value = "WebEdit"
oDesc("name").Value = "credit_card_number"
'Retrieve all WebEdit objects in this page
Set EditCollection = Browser("Book a Flight: Mercury").Page("Book a Flight: Mercury").ChildObjects(oDesc)
NumberOfEdits = EditCollection.Count
'Search for a specific WebEdit object and set its value
For i = 0 To NumberOfEdits - 1
If EditCollection(i).GetROProperty("name") = EditToSearch Then
EditCollection(i).Set ValueToSet
End If
Next
End Sub
Find Specific WebEdit Objects on a Web Page and Save Their Values in an Excel File
Sub ChildObjects_Example()
'The following example retrieves the collection of
'WebEdit objects that contain credit card numbers
'and inserts them in an Excel file.
Dim NumberOfEdits
'Create a Description object to help retrieve all
'WebEdit objects in a specific page.
2
![Page 3: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/3.jpg)
Set oDesc = Description.Create()
oDesc("micclass").Value = "WebEdit"
'Retrieve all WebEdit objects from this page.
Set EditCollection = Browser("Book a Flight: Mercury").Page("Book a Flight: Mercury").ChildObjects(oDesc)
NumberOfEdits = EditCollection.Count
'Use the GetROProperty to retrieve the "text" property
'(which in this case represents a credit card number),
'and insert all retrieved credit card numbers in an Excel file.
For i = 0 To NumberOfEdits - 1
CurrentCreditNumber = EditCollection(i).GetROProperty("text")
WriteToCreditCardNumberExcelFile (CurrentCreditNumber)
Next
End Sub
Verify the Number of Items in a List
Sub CheckProperty_Example()
'The following example uses the CheckProperty method to verify the
'number of items in a WebList. Note that if the original count
'is not equal to 15, an error message is displayed in the Test Results.
Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("what-to-do").CheckProperty "items count", 15, TimeLimitation
Item_Count = Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("what-to-do").GetROProperty("items count")
If Item_Count = 15 Then
3
![Page 4: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/4.jpg)
'Remove one item from the list
Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebButton("Remove Item From List").Click
'Check if the item was removed from the list
Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("what-to-do").CheckProperty "items count", 14, TimeLimitation
Else
Reporter.ReportEvent micFail, "Number Of List Items", "The item count in the list should be 15, not " & Item_Count & "."
End If
End Sub
Select a List Item By Specifying The Item Name
Sub Select_Example()
'The following example uses the Select method to select a city
'by its name in the list of possible departure cities.
Browser("Mercury Tours").Page("Find Flights").WebList("depart").Select "London"
End Sub
Select a List Item By Specifying The Item Index
Sub Select_Example()
'In the following example, the Select method is used to
'select a city by its index number in the list of possible
'arrival cities.
Browser("Mercury Tours").Page("Find Flights_2").WebList("arrive").Select "#4"
End Sub
4
![Page 5: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/5.jpg)
Select an Item from a List and Verify Its Selection
Sub Select_Example()
'The following example uses the Select method to select
'an item from a WebList object and then checks
'if the item and others were actually selected.
'The results of the check are sent to the Test Results.
Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").Select "Calvin Rip"
Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").ExtendSelect "Leather Jacket"
Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").ExtendSelect "Green"
'Check if all items that were selected in the previous steps are
'actually selected in the list and send
CurrentSelection = Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").GetROProperty("selection")
If CurrentSelection <> "Rugby Shirt;Leather Jacket;Boots" Then
Reporter.ReportEvent micFail, "ExtendSelect", "The list does not contain all of the selected items."
Else
Reporter.ReportEvent micPass, "ExtendSelect", "The list contains all selected items."
End If
End Sub
Select an Additional Item in a List by Name or Index
Sub ExtendSelect_Example()
'The following example uses the ExtendSelect method to add an item to
5
![Page 6: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/6.jpg)
'a multiple-selection list in the 1stFlights list.
VbWindow("frmMain").VbWindow("dlgFlights").VbList("lstFlights").ExtendSelect "C345"
'or
VbWindow("frmMain").VbWindow("dlgFlights").VbList("lstFlights").ExtendSelect 2
End Sub
Activate an Edit Box's Native Focus Method
Sub Object_Example()
'The following example uses the .Object property to activate an
'edit box's native focus method:
Set MyWebEdit = Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username").Object
MyWebEdit.focus
End Sub
Saves a List of Links to a File
Sub ToString_Example()
'The following example retrieves a list of links from the
'"Simple Web Example" page, creates the FileSystemObject,
'and uses the ToString method to write the test object (link) names (for example,
'"banner" and "contents") and test object type (for example, "Frame")
'to c:\Links.txt.
Dim fso, ResultFile, oDesc
'Get a collection of all Links in the "Simple Web Example" page
6
![Page 7: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/7.jpg)
Set oDesc = Description.Create
oDesc("Class Name").Value = "Link"
Set oLinkCollection = Browser("Simple XML Example from").Page("Simple Web Example").ChildObjects(oDesc)
'Create the FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
Set ResultFile = fso.OpenTextFile("c:\Links.txt", 2, True) ' 2 = Open file for writing
'Write to the file
For i = 0 To oLinkCollection.Count - 1
ResultFile.Write oLinkCollection.Item(i).ToString & vbNewLine
Next
ResultFile.Close
End Sub
Include the Test Object Name and Type in the Test Results
Sub ToString_Example()
'The following example uses the ToString method to insert
'the test object's name and generic type in the Test Results.
ObjectString = Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("who-to-do-it-with").ToString
'Use the ToString to return value to enhance the reporter.
Reporter.ReportEvent micDone, "We are selecting the fourth item in the " & ObjectString, ""
Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("who-to-do-it-with").Select "#4"
End Sub
7
![Page 8: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/8.jpg)
Retrieve the Value of a Property in an Edit Box
Sub GetROProperty_Example()
'The following example iterates through the WebEdit objects
'on a page, searching for a specific WebEdit object in order
'to set a value for it.
Dim EditToSearch, ValueToSet, NumberOfEdits
'This is the value of the 'name' property for the WebEdit we want to find.
EditToSearch = "credit_card_number"
ValueToSet = "6543210123456789"
'Create a description object to help retrieve all WebEdit objects in a specific page.
Set oDesc = Description.Create()
oDesc("micclass").Value = "WebEdit"
'Retrieve all WebEdit objects in this page
Set EditCollection = Browser("Book a Flight: Mercury").Page("Book a Flight: Mercury").ChildObjects(oDesc)
NumberOfEdits = EditCollection.Count
'Search for a specific WebEdit and set its value
For i = 0 To NumberOfEdits - 1
If EditCollection(i).GetROProperty("name") = EditToSearch Then
EditCollection(i).Set ValueToSet
End If
Next
End Sub
Retrieve the Values From a List of Names in an Array
8
![Page 9: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/9.jpg)
Sub GetItem_Example()
'The following example uses the GetItem method to retrieve the names
'of students from an array in order to send them their grades. First
'the example uses the GetTOProperty method to retrieve the number of
'items from the list of student names. Then it generates an array
'according to the list size, inserting the each of the student's names
‘into the array. It then uses the GetItem method to retrieve each
'student's name. Finally it uses a function to send grades to the students.
ListSize = Browser("ListOfStudentNames").Page("ListOfStudentNames").WebList("ListOfStudentNames").GetTOProperty("items count")
Dim NamesArray()
'Dynamically allocate an array according to the list size
ReDim NamesArray(ListSize - 1)
For i = 1 To ListSize
NamesArray(i - 1) = Browser("ListOfStudentNames").Page("ListOfStudentNames").WebList("ListOfStudentNames").GetItem(i)
Next
'The following function uses the array of student names in order to send grades to the students.
SendGradesByStudentName (NameArray)
End Sub
WEB TABLE METHODS
Perform an Operation on an Edit Box Inside a Table
Sub ChildItem_Example()
9
![Page 10: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/10.jpg)
'The following example uses the ChildItem method to set the second
'edit box from the FirstName table to Example.
Set WebEditObj = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").ChildItem(8, 2, "WebEdit", 0)
WebEditObj.Set "Example"
End Sub
Find the Number of Objects Contained in a Table
Sub ChildItemCount_Example()
'The following example uses the ChildItemCount method to count
'the number of edit fields in a cell in the sample table.
Dim NumEdit
NumEdit = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").ChildItemCount(1, 2, "WebEdit")
End Sub
Find the Number of Columns In a Table
Sub ColumnCount_Example()
'The following example uses the ColumnCount method to determine the
'number of columns in the first row of the OutboundFlights table.
NumColumns = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ColumnCount(1)
MsgBox "The number of columns in the row is " & NumColumns
10
![Page 11: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/11.jpg)
End Sub
Display the Contents of a Cell in a Message Box
Sub GetCellData_Example()
'The following example uses the GetCellData method to display the
'contents of the cell located in cell 1, column 1 in a message box.
Set Text = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlight").GetCellData(1, 1)
MsgBox "text contains" & Text
End Sub
Find All Employees That Live in the Same City
Sub GetCellData_GetRowWithCellText_Example()
'The following example retrieves the names of all employees that live in
'the same city as John Smith so that he can arrange rides home with them.
'First, the example finds the table row containing
'"John Smith". Then it checks the value of the CityColumnn cell to determine
'the city in which John lives. It searches the table cells to find all other
'employees that live in that city. Finally it uses the GetCellData method to
'return those employees names and, using a function, generates a list
'containing those names.
CityColumn = 4
NameColumn = 2
'Get the row number for employee 'John Smith'
11
![Page 12: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/12.jpg)
RowNumber = Browser("CorporateEmployees").Page("CorporateEmployees").WebTable("EmployeesTable").GetRowWithCellText("John Smith")
Set AccommodationsCity = Browser("CorporateEmployees").Page("CorporateEmployees").WebTable("EmployeesTable").ChildItem(RowNumber, CityColumn, "WebEdit", 0)
TableRows = Browser("CorporateEmployees").Page("CorporateEmployees").WebTable("EmployeesTable").RowCount
'Search for all employees that live in the same city as 'John Smith' and add them to his ride home list
For i = 1 To TableRows
Set CurrentCity = Browser("CorporateEmployees").Page("CorporateEmployees").WebTable("EmployeesTable").ChildItem(i, CityColumn, "WebEdit", 0)
If CurrentCity.GetROProperty("value") = AccommodationsCity.GetROProperty("value") Then
EmployeeName = Browser("CorporateEmployees").Page("CorporateEmployees").WebTable("EmployeesTable").GetCellData(i, NameColumn)
AddToJohnSmithRideHomeList (EmployeeName)
End If
Next
End Sub
Find a Cell with Specified Text and Click It
Sub GetRowWithCellText_Example()
'The following example uses the GetRowWithCellText method to
'find the row in the "OutboundFlights" table that contains the
'text "Price" and then find the cell in that row that contains
12
![Page 13: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/13.jpg)
'a link and click it.
row = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").GetRowWithCellText("Price", 2, 2)
Set Link = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ChildItem(row, 2, "Link", 0)
Link.Click
End Sub
Retrieve Properties to Check Whether a Link Object Has a Specific Property
'The following example checks whether the "url" property exists, and, if not,
'generates an error. The example calls the HasPropertyURL(LinkObj) function
'(defined below as commented text).
has_url_prop = HasPropertyURL(Browser("Browser").Page("Page").Link("Continue"))
If has_url_prop = False Then
Err.Raise 91
End If
'
'The following function, used in the above example, uses the GetTOProperties method
'to retrieve the list of properties and values for the Link test object in order
'to check if a Link test object has the "url" property.
'
'Public Function HasPropertyURL(LinkObj)
'
'PropertyToSearch = "url"
'PropertyExistInTO = False
13
![Page 14: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/14.jpg)
'Set PropertyCollection = LinkObj.GetTOProperties
'
'For i = 0 To PropertyCollection.Count - 1
'
' If PropertyCollection(i).Name = "url" Then
' PropertyExistInTO = True
' Exit For
' End If
'
'Next
'
'HasPropertyURL = PropertyExistInTO
'
'End Function
Retrieve Properties Used to Identify a Table Object
Sub GetTOProperties_Example()
'The following example uses the GetTOProperties method to retrieve
'the list of properties and values used to identify the FirstName
'WebTable.
Set TableDesc = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").GetTOProperties
End Sub
Display All Properties and Values of a Link Object
Sub GetTOProperties_Example()
14
![Page 15: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/15.jpg)
'The following example uses the GetTOProperties method to return a
'Property collection containing the test object description
'(properties and values) for the All kind of Link object.
'It then displays the property name and value of each property in
'the returned collection.
Set LinkObject = Browser("Index").Page("index").Link("All kind of")
Set Props = LinkObject.GetTOProperties
PropsCount = Props.Count
For i = 0 To PropsCount - 1
PropName = Props(i).Name
PropValue = Props(i).Value
MsgBox PropName & " = " & PropValue
Next
End Sub
Find the Number of Rows in a Table
Sub RowCount_Example()
'The following example uses the RowCount method to determine the
'number of rows in the Outbound Flights table.
NumRows = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").RowCount
' NumRows contains 5
End Sub
15
![Page 16: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/16.jpg)
QTP Script for connecting to MS Access.
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.provider="microsoft.jet.oledb.4.0"
con.open"d:testdata.mdb"
rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop
The database we are using here is MS Access.Before running this script create a table in MS Acess.
In the above script I used table called "emp" and column names as "v1" and "v2".
"d:testdata.mdb" is path of the table which we created.
The main use of this script is to use testdata of table(which is in database) in the application.
16
![Page 17: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/17.jpg)
In the above script we are passing values from database to Textboxes in Windows Application.
Similarly script for connecting to other 2 databases are
QTP Script for connecting to sqlserver.
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open"provider=sqloledb.1;server=localhost;
uid=sa;pwd=;database=testdata"
rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop
Script for connecting to oracle
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open"provider=oraoledb.1;server=localhost;
uid=scott;pwd=tiger;database=testdata"
17
![Page 18: Web List Methods](https://reader036.vdocuments.us/reader036/viewer/2022082604/55252560550346d36e8b4713/html5/thumbnails/18.jpg)
rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop
18