views:

405

answers:

3

I want to use dynamic object recognition (descriptive programming) to find the first button labeled "Delete" that occurs after some given text (eg, the first Delete button that appears after the text "Item XYZ-123"). I have a kludgy way to do it if both the text and the button are inside a single row of a webtable, but I was hoping for a more elegant or reliable solution (hopefully one that won't rely on tables).

I would prefer to avoid using the .Object property, since the documentation claims that the .Object property only returns DOM objects when you are testing within IE, and not within firefox.

Thanks!

+1  A: 

Assuming there is not an easier way to do it, you could try parsing the HTML. Find the search text in the HTML, and start searching the HTML from that point onward for a "Delete" button. You should be able to pull an id or some other identifying property from the HTML that you can use for the descriptive programming.

Do you have sample HTML and QTP code that we could look at to see more details? Perhaps there is an easier way.

Tom E
+1  A: 

Here's a solution that uses the sourceIndex attribute, note that sourceIndex is an IE only property but QTP simulates it on Firefox so the same script will work on both browsers. You can choose to use coordinate based properties like abs_x and abs_y if source_index doesn't fit the bill.

The code that follows answers the question as asked, making it into a general function is left as an exercise for the reader ;o)

''#1. Create description for locator text 
Set textD = Description.Create()
textD("micclass").Value = "WebElement"
textD("innertext").Value = ".*Item XYZ-123.*"

''#2. Find locator sourceIndex
set texts = Browser("B").Page("P").ChildObjects(textD)
Set text = texts(texts.Count-1) ' Take last text '
textIdx = text.GetROProperty("source_index") ' works for FF too '

''#3. Create description for button 
Set buttonD = Description.Create()
buttonD("micclass").Value = "WebButton"
buttonD("value").Value = "Delete"
Set btns = Browser("B").Page("P").ChildObjects(buttonD)

''#4. Find first button after locator text 
For i = 0 To btns.Count
    If btns(i).GetROProperty("source_index") > textIdx Then
        btns(i).Click ' Or whatever you want to do with it '
        Exit For
    End If
Next

Things to note about this solution:

  • It doesn't assume anything about the element containing the text, if you know that this is the entire text in the element you can remove the .*s and/or add an "html tag" for much better performance.
    • That's why we take the last element that fits the description, the first element will be the BODY etc.
  • In the text's description we have to specify "micclass" = "WebElement" because by default ChildObject filters out WebElements assuming that they are un-interesting.
Motti
Thanks! This is perfect.
Kimball Robinson
A: 

Let me rephrase the question first.

How to retrieve reference to an object contained within WebTable if row number is unknown but you have a unique key value to find the row?

That applies to buttons, checkboxes, comboboxes and any other object in table.

Implementation.

1) Find row

intRow = objWebTable.GetRowWithCellText(sKeyValueText, "Item") You can specify column by name or number

2) Retreive child object

Set objButton = objWebTable.ChildItem(intRow, intCol, "WebButton", 0) You can specify column by number only. The last parameter comes into effect if you have more than one button at the same cell.

Check some other technical examples in my blog (http://automationbeyond.wordpress.com/).

Albert Gareev
Not necessarily in a table--although that is a common expectation. Also, finding the right table too look within might be an issue on dynamic pages which show a variable number of tables. I am looking for a generalized solution, so looking for objects in a table first would make sense, but I would have to allow for the possibility that in some cases I am looking outside of tables...
Kimball Robinson
Check if tables have html id property (which is unique in value per page) defined. In this case you won't have a problem identifying the table.Overall, I'm concerned you can really have a <i>reliable</i> generalized solution without identifying the right table as the context of the text/button you are looking for is as much as important. If your script will find the right text in the wrong table and report it as PASS then it's even worth.
Albert Gareev