QTP’s mysterious Init method

One limitation of QTP that has always bothered me is the inability to reuse its web objects after the browser reloads. I spend countless cpu cycles parsing the DOM to find an element, grab enough properties to make it unique, build a description with those properties and finally get the right micobject. Then I perform some action and that reloads the page, and my object is useless. If I want to use it, I have to start the process all over again.

After years of struggle to find an answer, I packed some gear, hired a Sherpa and set off to find a guru who could give me an answer.

Wally LLama

The dialog went something like this.

Me: Great guru, I must know how to reuse an object after a page refresh.
Guru: Look to the object. Your answer is in it.
Me: eh?
Guru: The object; your answer is in it.
Me: That doesn't even make sense.
Guru: Go now. By the time you reach home, you will have solved the mystery.
Me: But I'm not good at riddles.
Guru: It was a play on words, but since you are too dense to understand I'll just come out and tell you. QTP's web objects have an undocumented method called .init. Use it to reinitialize an object ofter a page load.
Me: Oh, now I get it "in it" and "init". That's pretty clever.
Guru: Be warned, this is not a supported feature and must not be spoken of in the presence of HP support.
Me: gotcha.

My point here is that it turns out QTP has an undocumented method called init in all its web objects. When you call WebElement.init, QTP will find the object on the page again...even after the page has reloaded.

For example, this code passes as-it, but if you comment out the penultimate line, you will get an error on the last line.

Visual Basic:
  1. Set oBrowser = Browser("version:=inter.*")
  2. oBrowser.navigate "http://www.google.com"
  3. Set oWebEdit = oBrowser.WebEdit("name:=q", "index:=0")
  4. oWebEdit.Set "software inquisition"
  5. oWebEdit.submit
  6. oBrowser.sync
  7. oWebEdit.init
  8. oWebEdit.Set "wally llama"

I have been warned that this is not supported by HP, so if it doesn't work for you, don't call them.

Tags: , , , , , , , ,

4 Comments for “QTP’s mysterious Init method”

  1. Boyd Patterson Says:

    I did some looking at the WWWPackage.dll that ships with QTP, and the ‘Init’ method is essentially on the DispTestObject class. I believe this is the basis for all test objects used by QTP and not just web, so there may be other undocumented uses for this method.

    When building the IntelliSense support in Test Design Studio, I intentionally left off the Init method because it was undocumented, but will now add it to Web objects since you have found an excellent use for it. Great find!

    Reply to Boyd Patterson

  2. Reusing Web-Objects after the page reloads | AdvancedQTP Says:

    [...] post, code examples and knowledge were all taken from this article at Will Roden’s excellent QTP and QA blog - the Software Inquisition. Was this article [...]

    Reply to Reusing Web-Objects after the page reloads | AdvancedQTP

  3. spanish_biker79 Says:

    Good finding!!!! Have you tried to use it with the objects returned by ChildObjects() ?

    I’m trying this and QTP 9.5 does not seem to support it:

    Set oDesc = Description.Create()
    oDesc(”micclass”).Value = “Link”
    Set nOccur = baseURL.WebElement(”html id:=bip-navsub”).childobjects(oDesc)

    For i=0 to nOccur.count-1
    baseURL.Link(”name:=” &letter).Click
    baseURL.Sync
    ‘Here the object is lost, so I’ve tried

    nOccur.Init ‘Object doesn’t support this property or method: ‘nOccur.Init’

    nOccur(i).Init ‘Does not throw any error but the following line fails

    currentURL = nOccur(i).GetROProperty(”href”) ‘General run error. The server threw an exception

    Any idea why this could be? (I’m posting this in the SQA post you used for giving this tip as well… http://www.sqaforums.com/showflat.php?Number=214679 )

    Cheers!!

    Reply to spanish_biker79

  4. Interview with QTP Experts: Part 3 | Learn QTP Says:

    [...] Here is the post in [...]

    Reply to Interview with QTP Experts: Part 3 | Learn QTP

Leave a Reply