RefreshObject method instructs UFT to identify a referenced object variable when the object changes state.

RefreshObject method UFT

Why and Where Do We Use RefreshObject Method?

Have you ever created a variable in UFT that has been set to a test object? If you have worked on UFT for any length of time, I am sure you do.

Have you ever needed that same object variable when the application has refreshed and changed state? If yes, you would know what I am talking about here. UFT would be unable to identify that object variable.

For those who are clueless what we’re discussing here, let us understand this with the help of three simple scripts.

Script #1

This script does operation on the same object directly two times.

Browser("Demo - QTP Training").Page("Demo Page 1 - QTP Training").WebEdit("username").Set "Text1"

Browser("Demo - QTP Training").Refresh
Wait(5)

Browser("Demo - QTP Training").Page("Demo Page 1 - QTP Training").WebEdit("username").Set "Text2"

Script #1 runs successfully.

Script #2

This script first creates a variable which references the object and then does the operation.

Set MyUserName = Browser("Demo - QTP Training").Page("Demo Page 1 - QTP Training").WebEdit("username")
MyUserName.Set "Text3"

Browser("Demo - QTP Training").Refresh
Wait(5)

MyUserName.Set "Text4"

If you run Script #2, UFT will throw an error. The reason is that the Browser – as soon as it is refreshed – has changed state but the object variable MyUserName still refers the old object. It can’t find that old object reference and hence errors out. To fix the error, you just need to include RefreshObject method in the script.

Script #3

This script first creates a variable which references the object, refreshes the object using RefreshObject method when browser is refreshed and then does the operation.

Set MyUserName = Browser("Demo - QTP Training").Page("Demo Page 1 - QTP Training").WebEdit("username")
MyUserName.Set "Text3"

Browser("Demo - QTP Training").Refresh
Wait(5)

MyUserName.RefreshObject
MyUserName.Set "Text4"

The script runs successfully as expected.

Check the video below to see these three scripts in action.

Do you know of any other quirk in UFT? Share your experience in the comments section below.