Categories
Uncategorized

Brush Up some key VBscripting concepts

1. How to have Const value in VBScripting?

Constant: – value that never changes,we can create string/numeric constants in vbscript

Syntax:
Const VariableName = VariableValue

Example:

Const uftHelp = "Helping ppl learn"
Const age = 21
Const dateofStart = #14-02-2013# 'we need to put # for date/time constants

Key Notes:-

Vbscript has it is predefined set of constant values, which starts with vb, these constants are built into VBScript, and we don’t have to define them before using them.

Example:

Constant
Description
Value
vbBlack
Black Color
&h00
vbBinaryCompare
Perform a binary comparison.
0
vbSunday
Sunday
1
vbQuestion
Display Warning Query icon, in msgBox
32
vbCr
Carriage return.
Chr(13)
VbCrLf
Carriage return–linefeed
Chr(13) & Chr(10)
vbLf
Line feed.
Chr(10)
vbDate
Date subtype
7
vbTrue
True
-1
vbFalse
False
0
2. Which type of conditional statement we should use in Vbscripting?

Conditional statement: – We use them to control the flow of your script execution.

Types of Conditional Statement:-

1.If-Then-Else:-It evaluates a condition is True or False, and based on the result it controls the flow of execution of statements.

Syntax:-

If Condition Then
    Statement A
else
    Statement B
End If

Example:-


2.Select-Case
The Select Case structure provides an alternative to If…Then…ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If…Then…Else statement, but it makes code more efficient and readable.

Syntax:-

Select Case “Condition”
    Case : Match1
    Case : Match2
    Case Else : Non Match
End Select

Example:-


Select Case v/s If -Else 
Select 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. 
As 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.

Example:-

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.

Difference between select-case and if-else
Select-Case or IF-Else


3. Difference between Function and Procedure in Vbscript?
Why Sub or Function?
These are lines of code, which are grouped together to increase reusability and reduce maintenance and enhance our Framework architecture.
Difference:-
Syntax:-

Function v/s Procedure Syntax difference
Syntax of Procedure and Function 


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

Example:-

'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

Note:-
To call a Sub or Function we can use “Call” method but remember both the following calls are same:-

Call Sum (A, B)   
Or
Sum A, B

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:-

Cannot use Parentheses when calling a SUB error in UFT
Error message incase of missing call statement or Parentheses



4.How to acheive Error Handling in UFT? 

Approach 1:– Simply by using “if-else-end if” statement. We have already learned conditional statements above so we wont be discussing this method. 

Approach 2:– Using concept of “On Error Resume Next
On Error Resume Next:- We are telling UFT, move to the next step of execution in-case error occurs. 
It 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.

On 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. 

Example:- 

'**********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



SetLastError v/s GetLastError

Error handling in UFT
Add caption


Both are known as Utility statements(Like utility Objects).
GetLastError:– 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. 

Example:- 

GetLastError Example in UFT


SetLastError :- 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.
Example:- 

 SetLastError 9
msgbox GetLastError


Difference between Arrays and Dictionaries
Sendkeys in UFT
VbScript Class

Leave a Reply

Your email address will not be published. Required fields are marked *