Pages

Wednesday, March 2

How "On Error Resume Next" can be a problem in QTP?

In this post, we will look into the use of Error Handling 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' was not there, it would have 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 as input! What'll happen? Whole your test will be doing something unexpected and you will end up with nothing other then confusions and looking in the code for What went wrong..... :)



Example 2:

Assume either because of some issue or unexpected behavior of the application, 'Login' button is not displayed.

Now, if you run below code--

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
wait 2
Browser("mybrowser").Page("NEWPAGE").WebLink("NEXT").click

Expected Result: When you click on Login button, it should navigate to next page, where it will click on NEXT link.


Actual 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'. It will try to click on NEXT link, which is not appeared.

It will keep executing all the following steps of the script without any error. But actually it will not achieve anything because application is still stuck on first page.

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.. "On Error Resume Next" is 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), but as explained in above examples, it may work in wrong direction if not used properly.

In case of any queries, please leave your comments.