Saturday, November 21, 2009

QTP WebTable Object

WebTable Object is a table containing a variable number of rows and columns.

We will see most of the methods and properties of WebTable object with examples below.

Write the below code in a notepad and save it with .html extention. I saved it as "a.html" as can be seen in the below screenshot (in IE). Also note that first checkbox is named as team and second as team1 in the code. Most of the examples below are done using this below table.





CaptureBitmap Method

CaptureBitmap will save a screen capture of an object as a .png or .bmp image, depending on the specified file extension at the below mentioned location.

Browser("Browser").Page("Page").WebTable("html tag:=TABLE").CaptureBitmap "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\table.png"

You can write the above used browser and page objects in descriptive programming also. If you run the above line on the example table, it will take a screenshot of the WebTable and save it as a .png file in Sample Pictures folder.

Check Method

Checks whether the actual value of an item matches the expected value. As an example

Make sure that both IE with that table and QTP are open .
Click record.
Go to Insert (Menu) -> Checkpoint-> Standard Checkpoint.
Click on checkbox (QTP checkbox). Make sure you click on checkbox and not on text "QTP".
Object Selection: Checkpoint Properties Window opens with "WebCheckBox: team" highlighted. Click OK.
Checkpoint Properties window opens. Uncheck all checkboxes except "Checked". Click OK.
Click Stop in order to stop recording.

It recorded this below line :
Browser("Browser").Page("Page").WebCheckBox("team").Check CheckPoint("team")

And I changed it to:
a= Browser("Browser").Page("Page").WebCheckBox("name:=team").Check (CheckPoint("team"))
msgbox a

Now you can run the above two lines and see the value of a. If the QTP checkbox is checked it will show True else False.

Above you can use Descriptive Programming for all objects also. I have used Descriptive Programming only for WebCheckBox object.

CheckProperty Method

Checks whether the specified object property achieves the specified value within the specified timeout. Here we are checking if the WebCheckbox object’s checked property achieves a value of 1 i.e. if checkbox is checked and in our case it will show True as both checkboxes are checked. 20 is the time in milliseconds.

a=Browser("Browser").Page("Page").WebCheckBox("name:=team").CheckProperty ("checked", 1, 20)
msgbox a

ChildItem Method

Returns a test object from the cell by type and index. Here it is returning QTP checkbox and we are clicking it afterwards.

Set a = Browser("Browser").Page("Page").WebTable("html tag:=TABLE").ChildItem(2, 1, "WebCheckBox", 0)
a.click

ChildItemCount Method

Returns the number of objects of a specific type in the specified cell. Here it is returning the count of checkboxes. 2 is the row and 1 is the columns below.

a=Browser("Browser").Page("Page").WebTable("html tag:=TABLE").ChildItemCount(2, 1, "WebCheckbox")
msgbox a

ChildObjects Method

Returns the collection of child objects contained within the object. Here it shows the names of all the child objects one by one in a message box.

Set a = Browser("Browser").Page("Page").WebTable("html tag:=TABLE").ChildObjects

msgbox a.count

For i=0 to a.count-1
msgbox i & " " & a(i).getroproperty("name")
Next

Click Method

Clicks the object. Here it is clicking the first checkbox.

Browser("Browser").Page("Page").WebTable("html tag:=TABLE").WebCheckBox("name:=team").Click

ColumnCount Method

Returns the number of columns in the table. Below "2" is the row number.

msgbox (Browser("Browser").Page("Page").WebTable("html tag:=TABLE").ColumnCount(2))

FireEvent Method

Triggers an event. Here it is firing the “onclick” event for first checkbox.

Browser("Browser").Page("Page").WebTable("html tag:=TABLE").WebCheckBox("name:=team").FireEvent "onclick"

GetCellData Method

Returns the text contained in the specified cell.

msgbox (Browser("Browser").Page("Page").WebTable("html tag:=TABLE").GetCellData(1,1))

GetROProperty Method

Returns the current value of the test object property from the object in the application. Checks the checked property of first checkbox and returns it.

msgbox Browser("Browser").Page("Page").WebTable("html tag:=TABLE").WebCheckBox("name:=team").GetROProperty("Checked")

GetRowWithCellText Method

Returns the number of the first row found that contains a cell with the specified text. See what the below line shows in case of our example WebTable.

msgbox Browser("Browser").Page("Page").WebTable("html tag:=TABLE").GetRowWithCellText("Hello")

GetTOProperties Method

Returns the collection of properties and values used to identify the object. Below code runs for first checkbox and return its name and value as seen below in the screenshot.

Set a= Browser("Browser").Page("Page").WebTable("html tag:=TABLE").WebCheckBox("name:=team").GetTOProperties

msgbox a.count

For i=0 to a.count-1
msgbox (a(i).Name & " " & a(i).value)
Next




GetTOProperty Method

Returns the value of the specified property from the test object description. Clearly understand the difference between this and above method. Below line will show "team" in the message box when run.

msgbox Browser("Browser").Page("Page").WebTable("html tag:=TABLE").WebCheckBox("name:=team").GetTOProperty("name")

Output Method

Retrieves the current value of an item and stores it in a specified location. Below example shows retrieving the value and storing it in runtime data table.

Make sure that both IE with that table and QTP are open.
Click record.
Go to Insert (Menu) -> Output Value-> Standard Output Value.
Click on checkbox (QTP checkbox). Make sure you click on checkbox and not on text "QTP".
Object Selection: Output Value Properties Window opens with "WebCheckBox: team" highlighted. Click OK.
Output Value Properties window opens. Uncheck all checkboxes except "Checked". Click OK.
Click Stop in order to stop recording.

It recorded this below line:
Browser("Browser").Page("Page").WebCheckBox("name:=team").Output CheckPoint("team_3")

And I changed it to:
Browser("Browser").Page("Page").WebCheckBox("name:=team").Output CheckPoint("team_3")
msgbox a

I just added the message box line so that you can see the runtime value of "QTP Checkbox" in the Global data sheet, otherwise there is no need of message box.
Now you can run the above two lines and see the value of checkbox in Global data sheet. If the QTP checkbox is checked it will show 1, else 0.

See DataTable in the below screenshot:



RowCount Method

Returns the number of rows in the table.

msgbox Browser("Browser").Page("Page").WebTable("html tag:=TABLE").RowCount

ToString Method

Returns a string that represents the current test object. Below example shows the name of the test object (checkbox) and its type.

a=Browser("Browser").Page("Page").WebCheckBox("name:=team").ToString
msgbox a



WaitProperty Method

Understand WaitProperty.

Submit Method

Submits a form. This method simulates pressing the Enter key, while the focus is on the object specified in the statement.

Suppose this page is open:
http://newtours.demoaut.com/

Now if we run this below code in QTP:

Browser("name:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").Image("alt:=Sign-In").Submit

Then this below page opens:
http://newtours.demoaut.com/mercurysignon.php

Drag, Drop Method

Drag & Drop Example

SetTOProperty Method

Understand GetTOProperty & GetTOProperties above and here and try SetTOProperty yourself.

Exist Property

Checks whether the object currently exists in the open application.

a=Browser("Browser").Page("Page").WebCheckBox("name:=team").Exist

If a="True" Then
msgbox "True"
else
msgbox "False"
End If
msgbox a

Object Property

Accesses the internal methods and properties of the object. The Object property for Web objects is supported only when running steps on Internet Explorer. It is not supported when working with Netscape Browser or Mozilla Firefox. Below example accesses the checkbox object and then performs click method on it.

set a=Browser("Browser").Page("Page").WebCheckBox("name:=team").Object
a.click