Reusing Web Objects after the Page Reloads

Article Tools

Will Roden from the Software Inquisition writes about a major undocumented feature in QTP, which could potentially allow you to write far more elegant test than though possible to date.

One of the more frustrating limitations of QTP web test objects was that they went out of sync whenever the webpage reloaded. This means that if you held a reference to a web-object, you couldn’t could on it to work throughout your script.

For example, this script would break at the last line, since the reference was no longer valid after the webpage reloads:

Set oBrowser = Browser("version:=inter.*")
oBrowser.navigate "http://www.google.com" 'Just navigate to a demo page
Set oWebEdit = oBrowser.WebEdit("name:=q", "index:=0") 'Get a reference to a web object
oWebEdit.Set "software inquisition"
oWebEdit.submit 'This causes the page to reload
oBrowser.sync

'The next line would break
oWebEdit.Set "wally llama"

This means that you cannot count on your variables and references, thus making your script far less elegant than they could have been.

Well, luckily, Will found out an undocumented method which apparently solves the problem. By using a .Init command after the page loads, the web-object “resyncs”, and the script will not break:

Set oBrowser = Browser("version:=inter.*")
oBrowser.navigate "http://www.google.com"
Set oWebEdit = oBrowser.WebEdit("name:=q", "index:=0")
oWebEdit.Set "software inquisition"
oWebEdit.submit 'Page reloads
oBrowser.sync

'This "resyncs" oWebEdit:
oWebEdit.init

'Now the next line will work
oWebEdit.Set "wally llama"

 

This means that we can use references, variables, function, and everything we’d ever wanted – we just have to remember to use the .Init command, and all will be well.

 

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

Previous postGet Pixel Color Next postAdd New Methods to Objects

Related Posts

Post Your Comment

You must be logged in to post a comment.