Usually in keyword driven frameworks, we call the functions to perform specific operation like createOrder(), CancelOrder(). If error occurs in any of these functions, it is not important that we come back to main driver script and continue with next test case.
Code structure below demonstrates how we can do this in QTP.
'Code inside driver script.
On error resume next
'The advantage of the on error resume next is that we can continue to next statement if any error occurs in current statement.
Call createOrder()
If err.number <> 0 then
print err.description & err.source
End if
'*************************************************
'If error occurs at statement2, control of the script goes back to the caller. It does not execute any statment after statement2. This saves lot of time .
Function createOrder()
statement1......
statement2......
..........
more statements
End Function
**************************************************************************
Please note that if error occurs, control goes to next line of code and not the next block of code.
Example 1 -
on error resume next
a = 3/0
msgbox "hello"
msgbox "end"
'****************************************************************
Example 2 -
Consider below piece of code.
on error resume next
if (2/0) = 2 then
msgbox "true"
else
msgbox "false"
msgbox "end of code"
'************************************************************
Example 3 -
Consider below piece of code.
on error resume next
call myproc()
msgbox "end"
-------------------------------------------------------------------------------------
sub myproc()
if (2/0) = 2 then
msgbox "true"
else
msgbox "false"
end sub
Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.
Code structure below demonstrates how we can do this in QTP.
'Code inside driver script.
On error resume next
'The advantage of the on error resume next is that we can continue to next statement if any error occurs in current statement.
Call createOrder()
If err.number <> 0 then
print err.description & err.source
End if
'*************************************************
'If error occurs at statement2, control of the script goes back to the caller. It does not execute any statment after statement2. This saves lot of time .
Function createOrder()
statement1......
statement2......
..........
more statements
End Function
**************************************************************************
Please note that if error occurs, control goes to next line of code and not the next block of code.
Example 1 -
on error resume next
a = 3/0
msgbox "hello"
msgbox "end"
'****************************************************************
Example 2 -
Consider below piece of code.
on error resume next
if (2/0) = 2 then
msgbox "true"
else
msgbox "false"
msgbox "end of code"
'************************************************************
Example 3 -
Consider below piece of code.
on error resume next
call myproc()
msgbox "end"
-------------------------------------------------------------------------------------
sub myproc()
if (2/0) = 2 then
msgbox "true"
else
msgbox "false"
end sub
Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.
No comments:
Post a Comment
Please Leave your reply. We value your feedback and inputs