Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

Get Row/Column Of An Object in a WebTable

by Anshoo Arora on June 27, 2011

This article shows a simple way of finding out in which cell a QTP’s Test Object resides. I find the method shown in this article a more effective and faster way of finding the cell I’m looking for. I have found that this method performs better than traversing rows/cells of a WebTable using in-built WebTable methods such as ChildItem, ChildItemCount and GetCellData. Also, for test objects, it is also not possible to find an exact match with the methods provided for WebTable – it is only possible for strings.

A workaround to find objects inside WebTable using DOM table/tr/td is shown in this article. The code snippets below show how to retrieve the row and column of a given object from a WebTable.

Consider the element “Doe” in the table below. We can visually see that the link is in the 5th row (starting at the heading) and the 3rd column.

ID First Name Last Name Gender Attending class?
12345 John Smith M
12346 Anne Johnson F
12347 Adam Roberts M
12348 Mary Doe F
12349 Tiffany Smith F

 

Executing the following code will provide us our expected output:

Set element = Browser("creationtime:=0").WebElement("innertext:=Doe", "index:=0")

MsgBox "Row# " & element.GetRow()
MsgBox "Col# " & element.GetColumn()

The above example uses WebElement, but this approach works for all Web objects. Similarly, the code for finding the 2nd WebCheckBox for Anne Johnson will give us: Row# 3 and Column#5 with the following code:

MsgBox Browser("creationtime:=0").WebCheckBox("name:=attending", "index:=1").GetRow
MsgBox Browser("creationtime:=0").WebCheckBox("name:=attending", "index:=1").GetColumn

The Code

The following two methods can be used directly for the object and will retrieve ‘either’ the row or the column. The below method retrieves the Row for an object residing in a WebTable:

Find Row of the Object from WebTable
Function GetRow(ByVal TObject)
    GetRow = -1
 
    Set TObject = TObject.Object
 
    Do
        If TObject.nodeName = "TR" Then
            GetRow = TObject.rowIndex + 1
            Exit Function
        End If
 
        Set TObject = TObject.parentNode
    Loop Until TObject.nodeName = "HTML"
End Function
 
RegisterUserFunc "Image", "GetRow", "GetRow"
RegisterUserFunc "Link", "GetRow", "GetRow"
RegisterUserFunc "WebEdit", "GetRow", "GetRow"
RegisterUserFunc "WebList", "GetRow", "GetRow"
RegisterUserFunc "WebCheckBox", "GetRow", "GetRow"
RegisterUserFunc "WebElement", "GetRow", "GetRow"
RegisterUserFunc "WebRadioGroup", "GetRow", "GetRow"
Example
MsgBox Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").GetRow

This method returns the column of the object inside a WebTable:

Find Column of the Object from WebTable
Function GetColumn(ByVal TObject)
    GetColumn = -1
 
    Set TObject = TObject.Object
 
    Do
        If TObject.nodeName = "TD" Then
            GetColumn = TObject.cellIndex + 1
            Exit Function
        End If
 
        Set TObject = TObject.parentNode
    Loop Until TObject.nodeName = "HTML"
End Function
 
RegisterUserFunc "Image", "GetColumn", "GetColumn"
RegisterUserFunc "Link", "GetColumn", "GetColumn"
RegisterUserFunc "WebEdit", "GetColumn", "GetColumn"
RegisterUserFunc "WebList", "GetColumn", "GetColumn"
RegisterUserFunc "WebCheckBox", "GetColumn", "GetColumn"
RegisterUserFunc "WebElement", "GetColumn", "GetColumn"
RegisterUserFunc "WebRadioGroup", "GetColumn", "GetColumn"
Example
MsgBox Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").GetColumn

The below implementation retrieves both row and column as a Scripting.Dictionary.

Find Row & Column Info as Scripting.Dictionary
Function CellInfo(ByVal TObject)
    Dim dic
 
    Set dic = CreateObject("Scripting.Dictionary")
    dic.Add "row", -1
    dic.Add "col", -1
 
    Set TObject = TObject.Object
 
    Do
        If TObject.nodeName = "TD" Then dic("col") = TObject.cellIndex + 1
 
        If TObject.nodeName = "TR" Then
            dic("row") = TObject.rowIndex + 1
            Exit Do
        End If
 
        Set TObject = TObject.parentNode
    Loop Until TObject.nodeName = "HTML"
 
    Set CellInfo = dic
End Function
 
RegisterUserFunc "Image", "CellInfo", "CellInfo"
RegisterUserFunc "Link", "CellInfo", "CellInfo"
RegisterUserFunc "WebEdit", "CellInfo", "CellInfo"
RegisterUserFunc "WebList", "CellInfo", "CellInfo"
RegisterUserFunc "WebRadioGroup", "CellInfo", "CellInfo"
RegisterUserFunc "WebElement", "CellInfo", "CellInfo"
RegisterUserFunc "WebCheckBox", "CellInfo", "CellInfo"
Example
Set cells = Browser("title:=myBrowser").Link("innertext:=myLink", "index:=1").CellInfo()

MsgBox cells("row")
MsgBox cells("col")

cells.RemoveAll
Set cells = Nothing

I hope you find this helpful. As always, thanks for visiting Relevant Codes :)

{ 48 comments… read them below or add one }

1 Vimal June 28, 2011 at 9:34 am

Anshoo,

Again a wonderful topic from you. I had implemented this idea in my old project as said in the same way. Thought of writing it to my blog, but somehow I haven’t. Your efforts are appreciable.

Reply

2 Anshoo Arora June 28, 2011 at 2:12 pm

Thank you for your kind words Vimal. I searched the net a lot before making this post – I was afraid this may be a common technique which may already be available somewhere. But, because I was not able to find it, I thought to share it.

3 Prabhu June 28, 2011 at 10:24 am

Interesting and simplified way to deal with Webelements inside Webtable.

Reply

4 Anshoo Arora June 28, 2011 at 2:16 pm

Thank you Prabhu. Also, this approach can be used for any Web object that resides in the WebTable. All we have to do is replace the reference to the WebElement with any other object inside the table.

5 Kashyap July 5, 2011 at 6:14 am

Very good article.

It’s a shame that QTP will let us use the DOM properties using “object” only on IE browser.
If this could be used on Firefox, it would be great.

Because of this reason , this great approach can only work on IE window.

Reply

6 Anshoo Arora July 6, 2011 at 8:16 pm

Kashyap: What version of QTP are you using? QTP 11 supports Record/Playback and DOM commands with Firefox.

7 PJ July 8, 2011 at 10:35 am

Hi Anshoo

I have created wrapper function arround the original qtp functionality if i want to do any operation
like
Button click operation
Image click ,mouseover operation
Is it Good way to do qtp scripting?
Any suggestion to enhanced these Wrapper function?

Reply

8 Anshoo Arora July 16, 2011 at 11:49 am

PJ: I can only say that it will only be meaningful to create wrapper functions if they serve a greater purpose than existing QTP functions. If your button click operation ‘only’ clicks the button, then it will be redundant.

9 PJ July 18, 2011 at 2:50 am

Anshoo Arora July 16, 2011 at 11:49 am
PJ: I can only say that it will only be meaningful to create wrapper functions if they serve a greater purpose than existing QTP functions. If your button click operation ‘only’ clicks the button, then it will be redundant.

=>
the Error handling part is also done inside wrapper function.if error comes then execution of current test stop.

10 Nishanth July 9, 2011 at 6:52 am

Hi Anshoo,
I created a table checkpoint on the username and password edit boxes in the newtours.demoaut.com appln. There are2 columns and 5 rows. The 2row,1col contains constant UserName: and 3row,1col contains constant Password:. Now during run time I am inserting username as xyz123 and password as xyz123.
Now my question is…dont these values get updated in the 2nd row, 2nd col and 3rd row, 2nd col of the webtable respectively.
After inserting the username and password values I wrote the below 2 lines of code.
a = (Browser(“Welcome: Mercury Tours”).Page(“Welcome: Mercury Tours_2″).WebTable(“Registered users can sign-in”).GetCellData(2,1))
b = (Browser(“Welcome: Mercury Tours”).Page(“Welcome: Mercury Tours_2″).WebTable(“Registered users can sign-in”).GetCellData(2,2))
msgbox a ‘Returns UserName:
msgbox b ‘Returns a null value

why I am not getting xyz123 in the second msgbox? Please explain how this stuff works. Will be grateful.

Reply

11 Anshoo Arora July 17, 2011 at 7:46 pm

Nishant: What is the output you’re getting?

12 Nishanth July 18, 2011 at 2:21 pm

I am getting a null value(a blank message box)
I also have another question regarding webtables.
Scenario1:I have the application ready, I am recording on it and I want to insert a Table Checkpoint to verify a value in a particular cell. When the Table checkpoint property window opens I go through the table and if I see the value that I want to check then I select that cell. I run the test case and it executes the checkpoint. Passes or Fails. Life is easy
Scenario2: The application is ready but I want to do this programatically. How should I know which cell has the value in it?
And more importantly how should I know in which webtable is this cell present? Is it only by going thru one table after the other?
Scenario3: The application is not ready. How can I use webtables? Is it only by looking at source code?

Reply

13 Anshoo Arora August 1, 2011 at 3:56 pm

Nishanth,

Scenario 1, 2: The below code will dynamically retrieve values from all rows/columns from the table you’ve referenced:

With Browser("title:=.*Mercury Tours.*").WebTable("innertext:=Registered.*")
	iRows = .GetROProperty("rows")
	iCols = .GetROProperty("cols")

	For row = 1 to iRows
		For col = 1 to iCols
			Print .GetCellData(row, col)
		Next
	Next
End With

Scenario 3: Well, if you have the source code you can certainly do a lot. However, if the application is not ready means the required source code may not be available in the first place. It will be very tough to automate anything without having something concrete to begin with.

14 Reena July 28, 2011 at 12:59 am

Hi Anshoo,

Thanks for the code .
Iam beginner in QTP . Could you assit me , if I would need to add the above the method to the QTP library or do i need to use it directly ?
Mail me at reachreenadey@gmail.com
Thanks ,
Reena

Reply

15 Anshoo Arora August 1, 2011 at 4:16 pm

Reena: Please follow the steps below to add this function as to your test.

1. Create a new function library from File -> New -> Function Library
2. Add the above methods with the RegisterUserFunc statement to the library
3. Save it
4. Associate it with your test from File -> Associate Library ‘LibraryName’ with ‘TestName’
5. Save the test

16 QTPLearner August 22, 2011 at 11:02 pm

Can you help me in scripting of current SignOut for Gmail , which got changed by google…

Reply

17 Anshoo Arora September 12, 2011 at 10:16 pm

QTPLearner: For Gmail SignOut, please see if the below works:

Browser("title:=Gmail.*").WebTable("html id:=gbmpal").Link("innertext:=Sign out").Click

18 Anish 10110 September 12, 2011 at 8:48 am

Anshoo,

This is a very good article. Good to know about many different ways of using the same thing :) :)

As you told that ‘method performs better than traversing rows/cells of a WebTable using in-built WebTable methods’, does it mean that this is faster than the WebTable methods?

Reply

19 Anshoo Arora September 12, 2011 at 10:24 pm

Its certainly a very quick way to retrieve the row and column of a given object :)

20 nishanth October 7, 2011 at 7:22 am

HI Anshoo,

A basic question. When should we go for using WebTables?

thanks,
Nish

Reply

21 Anshoo Arora October 11, 2011 at 4:50 pm

Nishanth: Not sure what you mean? WebTables are in the target app..

22 nishanth December 7, 2011 at 8:07 am

I was asked this question in an interview. How do you verify if a particular mail is present in the Yahoo Inbox?. I started saying…”I will see if there is a property which holds the subject or some information which identifies this mail and…” . Even before I finished my answer the interviewer interrupted me and said, what if it is a webtable? I started telling him that if its a webtable then I will use the GetCellData, the Rows and Cols properties etc to get the value. Even before I could finish he interrupted again and said that it is indeed a webtable. His tone was like…”the yahoo inbox is a webtable…how come you dont know about it?”

I never worked on Yahoo Inbox so I never thought whether its a webtable or not.
Now my question is…just by looking at a web application do we come to know which is a webtable and which is not?

When should we go for using the webtable methods and properties? Sometimes to get a value of an object GetROProperty also works along with webtable methods? So when should we go for using webtable methods? what is the best scenario for using webtable methods and properties?

I am still in the process of learning QTP. I feel comfortable working with Webtables but I dont know when I shud use WebTables methods and properties. Please reply.

23 Prasath October 8, 2011 at 7:02 am

Hi Anshoo
Thanks for a wonderful explanation to retrieve row & column values.
How can we retrieve innertext property of all cells by row & column which is inside multiple webtables.
For eg: consider the following html snippet,

row1
rowvalue1
rowdesc1

row2
rowvalue2
rowdesc2

Thanks.

Reply

24 Prasath October 8, 2011 at 7:06 am

Sorry the above post with invalid HTML, here is the proper one..


” row1
” rowvalue1
” rowdesc1


” row2
” rowvalue2
” rowdesc2

“”

Reply

25 Dinesh October 11, 2011 at 3:10 pm

Hey Anshoo,
First of all thank you for sharing all your methods and approaches.
Here is a scenario i have, in a web table, I want to find out the first child objects present within a cell (row and col is input parameter). i.e in a cell (say row 2, col 2) i have some objects present which i will know only at runtime. it can be a check box or a radio button or a link..anything. Now how can i find the first child object present in the cell?

Once i find out the first child object, i will set that object as my control to perform my next operation.

How to do that? without using childobjects or childitem because here i need to mention the micClass to identify the object.

Please advice

Dinesh

Reply

26 nishanth November 11, 2011 at 9:00 am

Do you mean to say that one cell in the webtable contains more than one object and you want to find out the first object in that cell?

27 Dinesh November 11, 2011 at 7:03 pm

@Nishant.. Yes thats exactly what i want to do… do you have any suggestions?

28 Partha November 10, 2011 at 10:20 am

Anshoo,
I have a webtable with hundreds of rows and I am using a for loop to go throught the rows and perform actions based on conditions satisfied. When the condition is satisfied it comes back of the loop and start looking for other condition and go through the loop again starting from the top row again. I want to eliminate this, I want QTP to start looking for for the condition from the row where it was left. How to do this?

Thanks
Partha

Reply

29 Anshoo Arora December 27, 2011 at 6:43 am

You can store the last row in a variable and force the loop to start from there.. Example:

lastRow = 1

For ix = lastRow To WebTable.GetROProperty("rows")
    If conditionIsMet Then
        lastRow = ix
    End If
Next

30 Partha November 10, 2011 at 10:29 am

Example to the previous question:

For i = 100 to 1 step -1
Some condition
StrCompare ( comparing two strings)
condition satisfied
Exit for ( in the row 51)

Now I want the loop to start from 51
but it is starting from 100th row again.

Reply

31 nishanth November 11, 2011 at 8:40 am

I think one way to do this is to store the number in an environment variable and update the env variable whenever the condition is satisfied.

32 Dinesh November 11, 2011 at 7:01 pm

@ Nishant – Thats exactly i want to find out.
Do you have any suggestion?

Reply

33 vasanth November 21, 2011 at 2:20 am

what’s use of RegisterUserFunc?

Reply

34 Gaurav November 30, 2011 at 1:27 am

Thanks for this article. Now working with web tables has become easy .

cheers. Gaurav

Reply

35 prasad January 17, 2012 at 11:06 am

Assume there are 5 rows and 5 cols in one web table and all cols and rows are having data
I want script to read all data at a time

Reply

36 Anshoo Arora February 13, 2012 at 10:45 am

Prasad, see the GetCellData method of the WebTable. You will use that to loop each cell of the table to retrieve data. Something like the following:

For row = 1 to 5
    For col = 1 to 5
        Print Browser().Page().WebTable().GetCellData(row, col)
    Next
Next

37 Muzaffar January 25, 2012 at 12:19 pm

Hello Anshu!
Recording this page and using code snippets gives error “Object doesn’t support Browser(….”

how to solve this issue?

Reply

38 Piyush February 4, 2012 at 4:00 am

Hi Anshoo,
how wud we know that index=0 for this element and creationtime=0 for Browser in following step?
Set element = Browser(“creationtime:=0″).WebElement(“innertext:=Doe”, “index:=0″)

Thanks,
Piyush Kotiyal

Reply

39 Anshoo Arora February 13, 2012 at 12:10 pm

CreationTime of 0 means we’re pointing to the first open browser. CreationTime of 1 means we’re pointing to the second open browser and so on.

We determine Index by the internal (HTML) placement of the object. Index of 0 means, in the HTML source, that particular object is the first to appear.

40 John February 7, 2012 at 7:11 pm

Hi Anshoo, thanks for putting this post together. .nodeName, .parentNode and .rowIndex are going to save me a lot of time on my current automation project. Well done.

Reply

41 ARPITA February 9, 2012 at 1:38 am

Hi Anshoo,
I have a web table like u mentioned in top.In my table there are 27th row and 7 column.In 4th column i have different value like zzz,aaa,bbb,ccc,zzz .One can be multiple time like zzz .I have to search zzz from 4th column and click on a respective hyperlink which exist in 1st row .1st row is a number but link is there how to click that link?Can u suggest some answer?

Reply

42 Anshoo Arora February 13, 2012 at 12:17 pm

Arpita, have you tried using ChildItem? Does that solve this dynamic binding to the target object?

43 ARPITA February 9, 2012 at 1:51 am

Anshoo ,
Again Arpita here.I am giving you the code what i have written.But its not able to click the link
rowcount1 = Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebTable(“name:=TT”).RowCount
msgbox rowcount1
c = 4
For x = 2 to rowcount1
Y = Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebTable(“name:=TT”).GetCellData(x,c)
If Y = “ZZZ” Then
ID1 =Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebTable(“name:=TT”).GetCellData(x,1)
msgbox ID1
Browser(“micclass:=Browser”).Page(“micclass:=Page”).Frame(“Index:=2″).WebTable(“Name:=TT”).Link(“name:=ID1″).Click

End If

Next

Thanks
Arpita

Reply

44 Anshoo Arora February 13, 2012 at 12:18 pm

Arpita, does the following work:

Browser(“micclass:=Browser”).Page(“micclass:=Page”).Frame(“Index:=2″).WebTable(“Name:=TT”).ChildItem(1, 1, "Link", 0).Click

45 ARPITA February 14, 2012 at 3:40 am

Thanks Anshoo,
Your code is working fine

Reply

46 ARPITA February 14, 2012 at 6:52 am

Hi Anshoo,
I have a question.In a web page table is there.Browse button is there inside table.if we click that browse button then file will attach.But QTP doesn’t recognize that browse button.Only it recognize that table but not recognize inside the object means that button.
when i am using object spy its also recognize only table.Can u suggest me some idea how to click that object which is not recognize by qtp.
This is my code
Browser(“micclass:=Browser”).Page(“micclass:=Page”).Frame(“Index:=1″).WebTable(“name:=sss”).WebElement(“name:=detailtextdata”).Click.
Plz reply

Reply

47 Anshoo Arora February 16, 2012 at 10:51 am

Arpita, can you share the source code of the page or a sample page where I can test this?

Btw, have you tried using .ChildItem?

Browser().Page().WebTable().ChildItem(1, 1, "WebElement", 1).Click
Browser().Page().WebTable().WebElement("innertext:=Browser", "index:=1").Click

48 Anshoo Arora December 27, 2011 at 7:02 am

Nishanth: That remark is not justified. The guy wasn’t sure what he was talking about. What if somebody has never worked with the Yahoo app? What then? There is no way I can tell if its a WebTable or not without looking at the source. Just because it looks like a table does not mean it has to be it..

Be glad you didn’t get to work with that guy or you would come out learning very little. :)

Reply

Leave a Comment

Previous post:

Next post: