QA automation with QTP/UFT – tired of your scripts failing? Use this 1 quick tip!

Hello! Today, I have 1 amazing tip that will make your scripts more durable. Now here is the deal. A lot of automation script failures stem from not properly waiting on objects and elements to load on a page. When an application loads slowly, your QTP/UFT scripts wants to run as fast as it can and it tries to perform actions on elements that haven't even loaded. THIS WILL CAUSE YOUR SCRIPT TO FAIL! The great thing is, there is an efficient solution. Whenever performing an action on a page, you always want to make sure the object on which you want to perform the action, has loaded properly. I recommend waiting on these objects to load when navigating to ne pages. For example: If I just landed on a new page and want to click on link on that page, I have to make sure that link is loaded. I provide a link below so we can see how this works.

Test Link

Function to copy


Function  fun_WaitForAnyLink(strProperties)
waitTime = 180
iTime = time
dTime = 0
bFlag = false

Set objBrowserPage = Browser("Index:=0").Page("title:=.*")
arrStrProperties = Split(strProperties, ",")
arrCount = uBound(arrStrProperties) + 1
If arrCount = 1 Then

Do while (dTime <  waitTime) and bFlag = false

if objBrowserPage.Link(strProperties).Exist(1) then
bFlag=True
Exit do
end if
dTime=datediff("s",iTime, time)
loop

ElseIf arrCount = 2 Then
strProperty1 = arrStrProperties(0)
strProperty2 = arrStrProperties(1)

Do while (dTime <  waitTime) and bFlag = false

if objBrowserPage.Link(strProperty1, strProperty2).Exist(1) then
bFlag=True
Exit do
end if
dTime=datediff("s",iTime, time)
loop

ElseIf arrCount = 3 Then
strProperty1 = arrStrProperties(0)
strProperty2 = arrStrProperties(1)
strProperty3 = arrStrProperties(2)
Do while (dTime <  waitTime) and bFlag = false

if objBrowserPage.Link(strProperty1, strProperty2, strProperty3).Exist(1) then
bFlag=True
Exit do
end if
dTime=datediff("s",iTime, time)
loop
End If
End Function


1. Just copy and past the above function into your script. This function will wait for any link that you specify.

2. If we use object spy, we can see that the link has a unique property name:=Test Link. We can directly call our function with this property and value.

fun_WaitForAnyLink "name:=Test Link"

3. Run the script and you will see that the script has no issues. Make sure to only have this browser window open. CLOSE ALL OTHER BROWSERS.

4. To see how the function works when it doesn't find a link, change the name property to an incorrect property.

fun_WaitForAnyLink "name:=Test Link1"

Now run the script and watch it wait for 3 minutes until it finds this non existent link. You can adjust this 3 minute timeout depending on your personal automation needs. Pretty cool right? DO NOT EVER USE STATIC WAIT TIME. Use this function as an example to wait on all of your objects that need loading. The great thing about this function, is if the application performance slows down, this function will adjust and wait for the link. But if the application is running quickly, this function will not cause any delays. Static wait times such as Wait 40, will always wait 40 seconds whether your application is fast or not. THIS IS NOT THE RIGHT WAY TO AUTOMATE!

Lastly. This isn't a standard function. It works with different syntax. With this function, you can wait for a link using up to 3 property values, and any different properties. So if the link in your application does not have a name and it can only be called using the outertext and index, you can call the function like this:

fun_WaitForAnyLink "outertext:=Test Link,Index:=0"

Amazing right? This is why this function is called wait for ANY link.

Remember, when going to a new page on an application, use this function to wait for a specific element to load. If your element is a button, you can easily modify this function for buttons or any other objects as well. Please post your comments and questions below. We love all of your feedback!!

 

 

Pin It on Pinterest

Clef two-factor authentication