When we manually test a feature, we wait for the object to appear on screen before performing any action on it. Similarly, automated scripts wait for a certain pre-defined time before performing any action on the object
In the below code, you can see that we have a Const variable WAITFOR. This variable is used as a timer. The script will wait for a maximum of 10s for the object to be displayed.
Const WAITFOR = 10
Function objFound(object)
If object.exist(WAITFOR) Then
objFound = True Else objFound = False
End If
End Function
Pros:
Declare the constant in one file and access it throughout the framework.
Instead of using wait() and exist() functions all over the place, you can call objFound(object) and then perform the next action.
If objFound(Browser("name:=Google").Page("title:=Google").WebEdit("name:=q")) Then
Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Set "Isn't UFT an awesome Automation Tool?"
End If
Cons:
While the above solution works most of the time, there are instances where the application is a lot slower and the object takes more than 10s to load.
In case of negative Test Cases, the script must wait for entire 10s and then return False.
Solution:
For Positive Test Case
Use Do Loop Until the condition is true. Exist(0) will immediately return the Boolean value.
If objFound(Browser("name:=Google").Page("title:=Google").WebEdit("name:=q")) Then
Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Set "Is UFT an awesome Automation Tool?"
End If
Function objFound(object)
Do Loop Until object.exist(0)
objFound = True
End Function
For Negative Test Case
Use Do Loop While the condition is true. Exist(0) will immediately return the Boolean value.
If objNotFound(Browser("name:=Google").Page("title:=Google").WebEdit("name:=q")) Then
Msgbox "Object Not Found"
End If
Function objNotFound(object)
Do Loop While object.exist(0)
objNotFound = True
End Function
Using the above two functions, we can make our script foolproof and independent of the performance of AUT.
By Raman Sivasankar Sr. QA Engineer at Pacific Life
Read more at Linkedin