In post#13, we talked about there are 3 error handling statements in Visual Basic of Application (VBA), which can be used in TestPartner. However, NOT all of these statements can be used in QTP, since QTP uses the Visual Basic Scripting Edition (VBScript) scripting language (refer to post#26 for error handling in VBScript).
The 3 error handling statements in VBA are:
1, On Error GoTo line
This statement has been used in post#9.
This statement enables the error-handling routine that starts at line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to line, making the error handler active. The specified line must be in the same procedure as the On Error statement; otherwise, a compile-time error occurs.
2, On Error Resume Next (see post#13 for using “On Error Resume Next” with “On Error GoTo 0″)
This statement has been used in post#12.
This statement specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues. Use this form rather than On Error GoTo when accessing objects.
3, On Error GoTo 0 (see post#13 for using “On Error GoTo 0″ with “On Error Resume Next”)
This statement has been used in post#12.
This statement disables any enabled error handler in the current procedure (It doesn’t specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0.).
============================================
In post#13, I explained about how to use “On Error Resume Next” and “On Error GoTo 0″ together by analyzing the script from post#12.
In today’s post, I will explain how to use “On Error GoTo Line” in a loop with “Resume line“:
We have an array SheetData(1 To 5) which contains 5 data: A1, B22, C3, D4, E55 (B22 and E55 contains typos, they should be B2 and E5 instead.)
The original code was:
Sub Main()
...
For i = 1 To RowCount
ComboBox("Parent.Caption=Preferences").Select SheetData(i)
Next
...
End Sub
When running the original code, TestPartner will generate run-time errors when try to select SheetData(2)-B22 and select SheetData(5)-E55, since they doesn’t exist in the ComboBox (B2 and E5 does exist in the ComboBox).
The run-time error will interrupt the run, which is unpleasant. I would like to handle it this way: if SheetData(i) doesn’t exist in the ComboBox, then write in the test result that “SheetData(i) doesn’t exist in the ComboBox”, do not pop-up the error, and resume to run next i, i.e., try to select SheetData(i+1).
The final code looks like this:
Sub Main()
...
For i = 1 To RowCount
On Error GoTo ErrorHandler
ComboBox("Parent.Caption=Preferences").Select SheetData(i)
Jump:
Next
...
Exit Sub
ErrorHandler:
UserCheck "PackageName", False, "The " & SheetData(i) & " does NOT exist."
Resume Jump
End Sub
Now, we see how to use “On Error GoTo Line” in a loop – use “Resume line” to resume the loop run (The line argument is any line label or line number).
============================================
Below is a more complicated example about how to use “On Error Resume Next”, “On Error GoTo 0″, “On Error GoTo Line“, and “Resume line” together.
Sub Main()
...
'Select different Packages from the ComboBox,
'expand Protocol nodes, and check their measurements are enabled
For i = 1 To ColumnCount
For j = 1 To RowCount
If j = 1 Then 'First row in the Measurement sheet is package name
On Error GoTo 0 'Disables error handling in the current procedure
'in case SheetData(1, i) doesn’t exist in the application
On Error GoTo ErrorHandlerA
'Select the package
ComboBox("Parent.Caption=Preferences").Select SheetData(1, i)
Dim Package As String
Package = SheetData(1, i)
ElseIf j = 2 Then 'Second row in the Measurement sheet is protocol name
Dim ProtocolPath As String
ProtocolPath = "\Measurements\" & SheetData(j, i)
'Report error if the protocol name read from Excel sheet
'does NOT match the protocol name displayed in the workstation
On Error GoTo 0 'Disables error handling in the current procedure
'in case ProtocolPath doesn’t exist in the application
On Error GoTo ErrorHandlerB
'Expand Protocol nodes
TreeView("Parent.Caption=Preferences").SelectItem ProtocolPath, , tpTreeButton
Else
'If the Excel cell is not empty,
'check all the CheckBoxes beside measurement nodes
If SheetData(j, i) <> "" Then
Dim MeasurementPath As String
MeasurementPath = ProtocolPath & "\" & SheetData(j, i)
'Report error if the measurement name read from Excel sheet
'does NOT match the measurement name displayed in the workstation
On Error GoTo 0 'Disables error handling in the current procedure
'in case MeasurementPath doesn’t exist in the application
On Error GoTo ErrorHandlerC
TreeView("Parent.Caption=Preferences").SelectItem (MeasurementPath)
'Store the mouse y position after "SelectItem", which is always
'in the middle of the first charactor of the measurement name
Dim y As Integer
y = TreeView("Parent.Caption=Preferences").MouseY
'Handle the situation when the measurement name is longer than
'the width of the treeview window
On Error Resume Next 'Handle the situation when there is no scroll bar
TreeView("Parent.Caption=Preferences").Scroll 0, tpScrollHorizontal
On Error GoTo 0 'Disables error handling in the current procedure
'Search if the CheckBoxInTreeView.bmp (13*13 pixels) exists or not
'Write the results to result summary.
If TreeView("Parent.Caption=Preferences").BitmapExists("CheckBoxInTreeView", 58, y - 8, 17, 17) Then
UserCheck "CheckBoxInTreeView", True, "The " & Package & MeasurementPath & " CheckBox is checked"
Else
UserCheck "CheckBoxInTreeView", False, "The " & Package & MeasurementPath & " CheckBox is NOT checked"
End If
End If
End If
JumpB:
Next
JumpA:
Next
'Close application to reset its original state
Window.Close
'Erase array
Erase SheetData
Exit Sub
ErrorHandlerA:
UserCheck "PackageName", False, "The " & SheetData(1, i) & " does NOT exist in the Preference Panel - Measurement Tab."
Resume JumpA
ErrorHandlerB:
UserCheck "ProtocolName", False, "The " & SheetData(1, i) & "\" & SheetData(2, i) & " does NOT exist in the Preference Panel - Measurement Tab."
Resume JumpA
ErrorHandlerC:
UserCheck "ProtocolName", False, "The " & SheetData(1, i) & "\" & SheetData(2, i) & "\" & SheetData(j, i) & " does NOT exist in the Preference Panel - Measurement Tab."
Resume JumpB
End Sub