Let us discuss various Exit statements which are quite useful in automation testing Using UFT.These statements are part of Vbscripting(Latest version) and when implanted in the code provides better control on the flow of execution of automated Tests.
It is also used in Error handling as and when some exception occurs we can use ‘Exit’ statements.
Syntax:-
- Exit Do
- Exit For
- Exit Function
- Exit Property
- Exit Sub
Detail:-
1.Exit Do, it is used to Exit a Do….Loop Statement. It transfers the control to the statement following the loop statement.
Example:-
‘Code to Exit, when condition is meet
Dim iVar
iVar = 1
Do
‘Printing the value
Print iVar
‘Incrementing the value
iVar = iVar +1
‘Incase value is ‘5’ then exit do
If iVar = 5 Then
Print “Exit Do”
Exit Do
End If
Loop while iVar<10
Dim iVar
iVar = 1
Do
‘Printing the value
Print iVar
‘Incrementing the value
iVar = iVar +1
‘Incase value is ‘5’ then exit do
If iVar = 5 Then
Print “Exit Do”
Exit Do
End If
Loop while iVar<10
2.Exit For, It is used to Exit a For….Next Loop, again control is transferred to the statement following the Next statement
Example:-
‘Code to Exit, when condition is meet
Dim iVar
iVar = 1
For iVar = 1 To 10
‘Printing the value
Print iVar
‘Incase value is ‘5’ then exit do
If iVar = 5 Then
Print “Exit For”
Exit For
End If
Dim iVar
iVar = 1
For iVar = 1 To 10
‘Printing the value
Print iVar
‘Incase value is ‘5’ then exit do
If iVar = 5 Then
Print “Exit For”
Exit For
End If
Next
3.Exit Function, as the name indicated it is used to Exit the function and execution is transferred to the statement that called the function.
Example:-
‘Code to Exit, when condition is meet
Dim iVar
iVar = 1
Dim iVar
iVar = 1
‘FUnction call
fnExit iVar
‘Executing the next statement
Print “Out of the Function “
‘Function to Print the values
Function fnExit(iVar)
For iVar = 1 To 10
‘Printing the value
Print iVar
‘Incase value is ‘5’ then exit do
If iVar = 5 Then
Print “Exit Function”
Exit Function
End If
Next
End Function
4.Exit Property, Immediately exits the Property procedure in which it appears. Execution continues with the statement following the statement that called the Property procedure.
Example:-
Note to understand the Example , try to replace last line as objHelp.Name = “QTP”
Example:-
Note to understand the Example , try to replace last line as objHelp.Name = “QTP”
‘************Defining the code for Class*************
Class UFTHelp
Private sQTP
‘Setting the Property
Public property Let Name (sUFT)
sQTP = sUFT
Select case sqtp
case“UFT”
MSGBOX ” Exit Property used “
‘Exit Property used
Exit Property
MSGBOX “I am not executed “
case“QTP”
MSGBOX “No Exit property used”
MSGBOX “I am getting executed”
end select
End Property
End Class
‘************Code for using the Class************
Dim objHelp
‘Creating the Object of Class UFTHelp
Set objHelp = New UFTHelp
‘Setting the value of variable
objHelp.Name = “UFT”
5.Exit Sub, similar to Exit Function, difference lies in the fact that it is used to Exit an Sub rather than a function.
Important:-
Exit Do:-When used within nested Do…Loop statements, Exit Do transfers control to the loop that is one nested level above the loop where it occurs.
Exit For:- When used within nested For loops, Exit For transfers control to the loop that is one nested level above the loop where it occurs.