Quicktest Pro - find first button after matching text
-
09-09-2019 - |
Question
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!
Solution
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.
- That's why we take the last element that fits the description, the first element will be the
- In the text's description we have to specify "micclass" = "WebElement" because by default
ChildObject
filters outWebElement
s assuming that they are un-interesting.
OTHER TIPS
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.
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/).