How "On Error Resume Next" can be a problem in QTP?
'"On Error Resume Next" is used to handle errors in vbscript/QTP and usually very helpful. But many times, it might give you unexpected outcomes. Let's see how..
Example 1:
Function Example(var1 , var2)
On Error Resume Next
Example = var1/var2
On Error GOTO 0
End Function
msgbox Example(2, 0)
Run this code and see the result.
Result - Blank value is returned because of 'On Error Resume Next', which is incorrect! If 'On Error Resume Next' is not there, it must return an error code - 11, as division by zero.
Consider the situation where you are passing the above output as an input is some other function! What'll happen? Whole your test will do something unexpected and you..... :)
You'll just keep looking in the code for What went wrong!!
Example 2:
Suppose there is a bug in application, i.e. Login button is not displayed.
On Error Resume Next
Browser("mybrowser").Page("mypage").WebEdit("loginname").Set "abhikansh"
Browser("mybrowser").Page("mypage").WebEdit("password").SetSecure "mypass"
Browser("mybrowser").Page("mypage").WebButton("login").click
On Error GOTO 0
Result : Even though the Login button is not displayed on the page, there will be no runtime error and because steps inside the 'On error resume next' are not optional, it leads to incorrect result.
So what's the lesson? Shall I avoid to use 'On error resume next'?
No. Not at all! But be very careful while using.
Remember.. Its useful when you intentionally don't want to let errors occur i.e. any temporary browser specific error or printer error which is not about AUT (application under test).