Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Error Handling in QTP

As we have 3 types of error handling methods in pure VB or VBA language, we can easily handle our errors like Try/Catch functionality. They are, On Error Resume Next On Error GoTo 0 On Error GoTo <LABEL> But VBScript supports only 2. They are On Error Resume Next On Error GoTo 0 So here we are going to talk about these 2 handlers. 1. On Error Resume Next This enables the vb error handler and whenever dynamic run time error occurs, it will collect the error information and continue with the next immediate statement. This won't stop the script execution when error occurs. 1. 2. 3. 4. On Error Resume Next Dim a a = 5 / 0 'this will give run time error - Divide by zero error msgbox "Script not stopped."

5. msgbox Err.Description So here 2 msgbox lines got executed. At the same time we are able to get the error information like I did in line 5. So you can check the error numbers and you can control your scripts in better way like, 1. 2. 3. 4. 5. On Error Resume Next Dim a a = 5 / 0 'this will give run time error - Divide by zero error If Err Then msgbox "Error occurs: " + Err.Number + "; " + Err.Message + "; " + Err.Description

6. End If Here is the list of Error codes http://www.csidata.com/custserv/onlinehelp/vbsdocs/vbs241.htm Also we will get negative error numbers also. 2. On Error GoTo 0 This is the default handler in VB. This will disables the handler and collects the error information and throw the error in msgbox. Mostly no one will use this. Then, You can raise your custom error like, 1. 2. 3. On Error Resume Next Err.Clear 'Use this to clear previously stored error info Dim a

4. 5. 6. 7. 8.

a = 5 / 0 'this will give run time error - Divide by zero error If Err.Number <> 0 Then 'Err.Raise code, message, description Err.Raise 100, "Please change the value", "Division by Zero error occurs" End If

Don't forget to put

Err.Clear as your error object may contain previously stored error info.
Language Reference

VBScript Error Messages

Error Code 5 6 7 9 10 11 13 14 28 35 48 51 53 57 58 61 67 70 75 76

Message Invalid procedure call or argument Overflow Out of memory Subscript out of range Array fixed or temporarily locked Division by zero Type mismatch Out of string space Out of stack space Sub or Function not defined Error in loading DLL Internal error File not found Device I/O error File already exists Disk full Too many files Permission denied Path/File access error Path not found

91 92 94 322 424 429 430 432 438 440 445 446 447 448 449 450 451 453 455 457 458 500 501 502 503 1001 1002 1003 1004 1005 1006

Object variable or With block variable not set For loop not initialized Invalid use of Null Can't create necessary temporary file Object required ActiveX component can't create object Class doesn't support Automation File name or class name not found during Automation operation Object doesn't support this property or method Automation error Object doesn't support this action Object doesn't support named arguments Object doesn't support current locale setting Named argument not found Argument not optional Wrong number of arguments or invalid property assignment Object not a collection Specified DLL function not found Code resource lock error This key already associated with an element of this collection Variable uses an Automation type not supported in VBScript Variable is undefined Illegal assignment Object not safe for scripting Object not safe for initializing Out of memory Syntax error Expected ':' Expected ';' Expected '(' Expected ')'

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1037 1038

Expected ']' Expected '{' Expected '}' Expected identifier Expected '=' Expected 'If' Expected 'To' Expected 'End' Expected 'Function' Expected 'Sub' Expected 'Then' Expected 'Wend' Expected 'Loop' Expected 'Next' Expected 'Case' Expected 'Select' Expected expression Expected statement Expected end of statement Expected integer constant Expected 'While' or 'Until' Expected 'While', 'Until', or end of statement Too many locals or arguments Identifier too long Invalid number Invalid character Unterminated string constant Unterminated comment Nested comment Invalid use of 'Me' keyword 'Loop' without 'Do'

1039 1040 1041 1042 1043 1044 1045 1046 32766 32767 32811

Invalid 'Exit' statement Invalid 'For' loop control variable Name redefined Must be first statement on the line Can't assign to non-ByVal argument Can't use parens when calling a Sub Expected literal constant Expected 'In' True False Element not found

Error Handling in QTP


Yet we know about Recovery Scenario & how to use them in our scripts, there is one more magic of qtp by which we can avoid errors in script.This is called error handling.

On Error statement enable or disable error handling. Now we will discuss two of the error handling stamenst here 1) On Error Resume Next 2) On Error Goto 0 Write bellow mentioned code in notepad & save it as A.vbs now run it with command prompt or double click it. msgbox "I am Good 1" call one msgbox "I am Good 2" function one() on error resume next msgbox "Function One start" happy msgbox "Function one end" end function msgbox "I am Good 3" This works fine,now do one thing comment on error resume next line now again run it what happen an error displayed Type Mismatch Happy, yes now I hope you got it what on error resume next does . Actually it is ignoring any error commencing in code or in your script. As in above example when it reaches to line 7 it looks for happy which is not declared anwahere so it displays Type Mismatch error but as we use on error resume next Whereas any error displayed in code it just ignores it & move forward for next line. So with on error resume next error is not corrected it is just ignored & our test continues.

Moreover on error resume next is local to a function in which it is called, it will be in effect as long as function is executes and will be nullified when the function finishes execution. For example : msgbox "I am Good 1" call one msgbox "I am Good 2" function one() on error resume next msgbox "Function One start" happy msgbox "Function one end" end function function two() on error resume next msgbox "Function two start" happy msgbox "Function two end" end function msgbox "I am Good 3" so this way we see that this statement is private to each function. Err Object : Whenever there is a run time error in the program , the properties of an Err object is filled with information which help to identify & handle error. After an On error resume next statement the Error objects properties are reset to zero or zero-length strings ().This error object is an intrinsic it means you need not to declare it in your code before you use it. msgbox "I am Good 1" call one msgbox "I am Good 2" function one() on error resume next msgbox "Function One start" happy msgbox "Error Number Is =" &err.number& "and" & "Error Description= " &err.description msgbox "Function one end" end function msgbox "I am Good 3"

QTP Parameterization with Data Table


by SEETARAM on OCTOBER 15, 2011

When a test is recorded using QTPs recording engine all the values are hardcoded. If we want to run the same test many times, the application might throw error because of duplication of data. Hence, the data needs to be parameterized from an external means. QTP provides an excellent feature called Data Table for this purpose. To demonstrate this feature, let us take an example of Flight sample application. Create a new test as below: Record Insert New Order Scenario (Refer Insert New Order) Replace the From with Origination parameter Replace the To with Destination parameter Replace the Name with CustomerName parameter Open Data Table: Click View > Data Table Double click first column header A and change the name to CustomerName Double click second column header B and change the name to Origination Double click third column header C and change the name to Destination Now we have three parameters in the data table. We just need to insert the values in all the three columns
Karthik Denver Frankfurt

Sam Rajini Ranjana Vinay Siva Vikram Rajesh Vikram Rekha

Frankfurt London Los Angeles Paris Portland San Francisco Seattle Sydney Zurich

London Denver Portland San Francisco Los Angeles Paris Zurich Seattle Sydney

Save the Test as InsertOrderParameterization Below is the code snippet for the test:
'Script - "Insert New Order" in the Flight application 'CustomerName, Origination, and Destination are parameterized through DataTable ' Purpose - Demonstrates Parameterization 'Author - Seetaram Hegde 'Copyrights - All rights reserved SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Progr

am Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open" Dialog("Login").WinEdit("Agent Name:").Set "admin" Dialog("Login").WinEdit("Password:").SetSecure Crypt.Encrypt("mercury") Dialog("Login").WinButton("OK").Click Window("Flight Reservation").ActiveX("MaskEdBox").Type "121212" Window("Flight Reservation").WinComboBox("Fly From:").Select Datatable.Value("Origination","Global") Window("Flight Reservation").WinComboBox("Fly To:").Select Datatable.Value("Destination","Global") Window("Flight Reservation").WinButton("FLIGHT").Click Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click Window("Flight Reservation").WinEdit("Name:").Set Datatable.Value("CustomerName","Global") Window("Flight Reservation").WinButton("Insert Order").Click Window("Flight Reservation").ActiveX("Threed Panel Control").Check CheckPoint("Threed Panel Control") Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"

As we can see in the above code, Replace the value in WinComboBox("Fly From:").Select "Denver" with Datatable.Value("Origination","Global") Replace the value in WinComboBox("Fly To:").Select "Frankfurt" with Datatable.Value("Destination","Global") Replace the value in WinEdit("Name:").Set "Karthik" with Datatable.Value("CustomerName","Global") Save the test.

We also need to set the Run options to Run on all rows by, Click File > Settings > Run > Select Run on all rows

The above setting will tell the QTP to run the test for multiple sets of test data. Since there are 10 sets of data stored in the Data Table of the test, the Test Script will run for 10 iterations and we can get the result for all the iterations.

Execute the test now with F5 or Run. Evaluate the result once execution is over.

The above figure shows the result of the test after execution, which contains the status of all the iterations (from 1-10). The Test Scenario Script what we have created just now demonstrates how we can utilize the Data Table feature of the QTP (QuickTest Professional) to run a test against multiple sets of Test Data.

You might also like