Brush Up some key VBscripting concepts – User friendly Tech help

n

nSelect Case v/s If -Else 
nSelect Case structure evaluates an expression once at the top of the structure. In contrast, the If…Then…ElseIf structure can evaluate a different expression for each ElseIf statement. 
nAs a good practice we can replace an If…Then…ElseIf structure with a Select Case structure only if each ElseIf statement evaluates the same expression.
n
nExample:-
n
In case of below code we would recommend using Select-Case Approach, as with if else we need to do comparison at each step to reach the required match, contrary to this we can directly reach the corresponding case in Select-Case, but remember this works only incase we are evaluating the same condition.

n

n

n

n

n

n

n

n

Select-Case or IF-Else

n

n3. Difference between Function and Procedure in Vbscript?
nWhy Sub or Function?
nThese are lines of code, which are grouped together to increase reusability and reduce maintenance and enhance our Framework architecture.
nDifference:-
nSyntax:-

n

n

n

n

n

n

n

n

Syntax of Procedure and Function 

n


Return:-
nFunction returns a value while Sub(Procedure) do not return any value.
n
Example:-

n

'Calling Sub procedure 
Call Sum_Sub(1,2)
'Taking Return fron Function
Res = Sum_fn(2,3)Msgbox Res
'I am Sum Procedure
Sub Sum_Sub(A,B) msgbox A+B
End Sub
'I am Sum Function
Function Sum_fn(A,B) Sum_fn = A+B
End Function

n

Note:-
nTo call a Sub or Function we can use “Call” method but remember both the following calls are same:-
n
nCall Sum (A, B)   
nOr
nSum A, B

n

It is because when we are calling a Sub with parenthesis then it required to use “Call” statement otherwise we can simply call it with name and parameters without any “Call”.Incase we missed this be ready for this error:-

n

n

n

n

n

n

n

n

Error message incase of missing call statement or Parentheses

n

n
n4.How to acheive Error Handling in UFT? 
n
nApproach 1:– Simply by using “if-else-end if” statement. We have already learned conditional statements above so we wont be discussing this method. 
n
nApproach 2:– Using concept of “On Error Resume Next
nOn Error Resume Next:- We are telling UFT, move to the next step of execution in-case error occurs. 
nIt helps in execution to continue despite a run-time error.Similar to the concept of try-catch in Java. Further we can check the error code to handle the flow of execution.
n
nOn Error Goto 0 :– It is used to disable the scope of error handling.Like curly braces used in try-Catch {} it limits the scope of error handling code. 
n
nExample:- 
n

n

'**********Error Handling Begins**************
On Error Resume Next
'Divide by 0
Dim y : y = 1/0
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)Err.Clear
'Ending the Scope of Error Handling
On Error Goto 0
'**********Error Handling Ends**************
'***********Code with no error handling
Dim z : z= 1/0

n



SetLastError v/s GetLastError

n

n

n

n

n

n

n

n

Add caption

n


Both are known as Utility statements(Like utility Objects).
nGetLastError:– It returns the error code for the most recent occurred error. Note:- We can use “DescribeResult” statement to return a text description of the specified error code. 
n
nExample:- 

nn

nSetLastError :- It is used to raise the given error in the test execution.It can be beneficial like sometime we need to raise the user-specific errors to control the flow of scripts.
nExample:- 
n
 SetLastError 9
nmsgbox GetLastError

n
Difference between Arrays and Dictionaries
nSendkeys in UFT
nVbScript Class

Was this article helpful?
YesNo

Similar Posts