Login   /   Register

Using web anchors to automate non-unique controls

Rate this article
     6 votes, average: 4.67 out of 56 votes, average: 4.67 out of 56 votes, average: 4.67 out of 56 votes, average: 4.67 out of 56 votes, average: 4.67 out of 5
Loading ... Loading ...
April 28th, 2009 by Yaron Assa

This very interesting discussion in out forums convinced me to write an article explaining my approach to automating non-unique web (and other environments) controls.

Non-unique Web Controls

If the world was perfect, our application would have unique, well named controls, all easily identifiable and immediately usable by our scripts. More often than not, though, our applications are a mess of unordered controls, with cryptic names, who are deemed non-standard by QTP. In the worst cases, we’re stuck with unnamed non-unique controls, who are only identified by their infamous Index property. And we all know that we should never ever ever trust the Index property.

Here’s an example for such a case:

First
Second
Third
Forth

While it’s absolutely clear to us which WebEdit goes with which label, QTP is completely blind to the connection between the two, and treats all the WebEdits as non-unique unrelated controls. Try to add them to your object repository, and you’ll get stuck with WebEdit, WebEdit_2, WebEdit_3, And WebEdit_4, with only the Index property to set them apart. If it were a table, we could’ve used the GetRowWithCellText and ChildItem commands to build some sort of workaround, but this structure isn’t even a proper table.

But do not despair, a solution will soon follow!

The content you are accessing is for registered users only
Registeration is FREE, quick and private.
You can either Register or Login if you have already registered

Other cases and environments

The example we’ve explored is relatively easy. The HTML code was clean and simple, and the path from the anchor to the target was self evident. Real world cases are usually much more complex and obscure, but the basic methodology holds:

Find an anchor, try to establish its relationship with the relevant target, track that relationship through the runtime objects, and formulate it into code.

Use that to get the target’s unique ID, and reach it with QTP.

In cases where the relationship is very tangled up (like javascript trees, for example), don’t even try using the HTML code, and go straight to tools like DebugBar, Firebug, and others.

Is this workaround unique to the Web environment? What about other environments such as .Net and Java?

Well, almost any environments which gives us access to Runtime Objects has similar hierarchical structure and properties. In .Net, for example, we can use the .Parent and .Controls properties to move up and down the application hierarchy, and the .Handle property to describe the target control in QTP (through the “hwnd” TestObject property).

And just like we have DebugBar and Firebug to help us with the HTML hierarchy, we have QTP’s built in .Net spy, as well as external open-source spy tools to help us in .Net and Java.

Actually, other environments usually allow us to accomplish ever greater goals than those described in this article: You can read our article on automating .net custom controls for a more elaborate use of the .Net runtime objects capabilities.

Posted in web

12 Responses to “Using web anchors to automate non-unique controls”

  1. Motti Says:

    [-]

    Very cool!!

    But please note that uniqueID is an IE only DOM property :(.

  2. Yaron Assa Says:

    [+]

    Currently only IE allows QTP to show the runtime objects, so as you say in mathematics: without loss of generality, let's assume t... ...

  3. daniva Says:

    [+]

    it's just an excellent article. i am impressed from the depth of the descriptions and examples. as well as the high level of the ... ...

  4. Kiwiwi Says:

    [+]

    Nice method, although I prefer to do this with reusable functions: (simplified:) call getWebElementWithText(oPage, oWebelement... ...

  5. barak3d Says:

    [+]

    Excellent tip with the uniqueID, I never used it in that way before, it was always my problem: to get the QTP object back after yo... ...

  6. barak3d Says:

    [+]

    By the way you could have used the parentElement method, and then use the getelementsbytagname("INPUT") method on the parent to ge... ...

  7. rajivkumarnandvani Says:

    [+]

    Good job same thing i used in Web Tree control http://rajivkumarnandvani.wordpress.com/2009/04/08/web-tree-control-vb-script-qtp... ...

  8. Devendra Sharma Says:

    [+]

    Hi Yaron Assa, I am very impressed from the depth of the descriptions and examples of your article: “Using web anchors to autom... ...

  9. anshooarora Says:

    [+]

    Thanks for sharing this amazing technique with us, Yaron. I haven't yet had a chance to incorporate this technique in one of our p... ...

  10. Sanjana Says:

    [+]

    I am getting an error in this line in QTP 9.2 MsgBox Browser("index:=0").Page("index:=0").WebElement("innertext:=First", "html ta... ...

  11. sgopakumar Says:

    [-]

    Can I re-use the same snippet for clicking on an web Image?

  12. rajkanwarsingh Says:

    [+]

    Hi, The Approach presented is really great. But I am facing the same issues as others have commented above and is as follow... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 1132 times