Tuesday, October 9, 2012

On Error Resume Next

   Hello All! Happy to be in touch with you all through this blog. In this post, we are going to see a way of error handling. On many occasions, the execution has stopped if any error occurs during exection. But in some case, we wanted to continue the execution even if any error occurs. QTP supports certain methods which allow scripts to run even if certain errors occurred.

On Error Resume Next

We need to include 'On Error Resume Next' at the beginning of the script or before the step in which we are anticipating some errors. This command will handle the error and continue the execution of the script.

Considering the below script, I know the value of 'a' but the value of 'b' is not constant and it may change based on other steps in the script.

a = 10
b = <Dynamic value>
Msgbox a/b

If the 'b' value is '0' then 'Division by Zero' error will be displayed. In this situation, we can add error handling command to handle this.

On Error Resume Next
a = 10
b = 0
Msgbox a/b
If Err.Number <> 0 Then
 Msgbox Err.Description
End If


With this example, the error will be handled and the actual error message will be displayed in the message box. The script will continue to execute without showing error/exit.

Now, we have activated this feature and If we don't want to have this enabled further, we can deactivate this error handling feature by below command.

On Error Goto 0

Putting all to gether,

On Error Resume Next
a = 10
b = 0
Msgbox a/b


If Err.Number <> 0 Then
 Msgbox Err.Description
End If


On Error Goto 0
Msgbox a/b


While executing this script, when first time error occurs it will be displayed in the Message box. Because at the first step we enabled the error handling feature. At the 10'th step we disabled this feature. So error will not be handled for all the steps that executed after Step 10.


Thursday, October 4, 2012

ExecuteFile function


   Hello! ExecuteFile is a VB script function which is used to execute any VBS (VB Script) file. It is also used to dynamically mapping a VBS file to a test.

Syntax : ExecuteFile "FileName with Path"

Example : ExecuteFile "C:\Test.VBS"

   You may ask that why do we have to use ExecuteFile function instead of mapping an VBS file to the Test. It depends on the situation and based on the use of the VBS file.

   Say for Instance, we have library (VBS) file which contains all common functions for a Test. It is important to note that this VBS file may required any part of the test as it contains all the common functions. So it is always better to map the VBS file to the test. All the library files that mapped to the test will be executed at the beginning of the exection.

   There may be a different case in which it is required to exectue a VBS file only on a specific scenario or a step. In this case, we can use ExecuteFile method to execute the script at the required step.