VB Scripting in UFT–An Excellent Guide for Beginners

Testing is now the most essential phase in software development process to ensure the quality of the product. Also, without having testing, it will be very risky to release the software to use. The test automation process has changed the testing dimension with respect to effort savings and lightning fast regression cycle. There is many test automation tools are used in the testing industry. But, the most used automation testing tool is UFT.  

In this VB Scripting in UFT article, below topics will be explained with examples –

· Variables in UFT

· Constants in UFT

· Array in UFT

· Conditional Statements in UFT

· Looping Statements in UFT

· Sub and Function Procedures in UFT

UFT Tutorial – Table of Content

UFT Tutorial #1: UFT Overview

UFT Tutorial #2: UFT Setup – Download, Install, License Configuration and ALM Connection

UFT Tutorial #3: UFT Object Repository

UFT Tutorial #4: UFT Actions & Function Library 

UFT Tutorial #5: UFT Parameterization 

UFT Tutorial #6: VB Scripting in UFT

UFT Tutorial #7: Step by Step Guide to Create Test Case in UFT

UFT Tutorial #8: Exception Handling in UFT

UFT Tutorial #9: Recording in UFT with Checkpoints & Dictionary Object 

UFT Tutorial #10: UFT Interview Questions and Answers 

UFT Tutorial #4: VB Scripting in UFT

VB Scripting in UFT:

UFT supports VB Scripting as the programming language to write the code for any test cases. VB Script is derived from Visual Basic 4.0 language, which was developed by Microsoft. VB Scripting in UFT is very easy to learn, and it can be used to develop both server-side and client-side scripting.

Variables in UFT:

Variables in UFT are the placeholder that denotes a memory location that stores program information. It can be changed during run time. A variable has been represented by its name to read/write the stored value. Only one type of variant is available in VB Scripting, which can interact with numbers, strings, or dates. Based on the data, the variable is acted accordingly.

If we store any number with the double quotation, then it’s acted as a string type variable. VBScript in UFT allows us to declare the variables with the help of below three statements –

  • Dim
  • Public
  • Private

Dim Statement – Dim is used to declare any variable immediately irrespective of the datatype. One dim statements allows to declare multiple variables which are separated by comma. The scope of this type of variable is within the code block( in the Case of UFT, scope is restricted to the corresponding actions or function or sub).

Dim myVariable

 Dim myVariable1, myVariable2, myVariable3

We can declare any variable immediately by using its name in the code. So, in this case, an additional declaration statement is not required. But, if we keep the statement “Option Explicit” at the beginning of our scripting area, we have to write the variable declaration statement. Otherwise, UFT will throw an error message.

Public statement – If we declare variables as public, the variables are available throughout all the scripts in the suite.

Private statement – If we declare variables as private, variables are available within the script itself.

Assigning Values to Variables:

Using the below structure, we can assign values to the variables using the equal symbole – 

variableNameString = “My Value” //Assign string

variableNameNumber = 1234 //Assign number

Here variable name should be kept on the left side of the equal symbol, where the value should be kept on the right side. For String, the value should be kept within double quote marks.

Scalar Variables – When we store a single value to the variable, it’s represented as Scalar Variable.

Array Variables – When string more than one value in a single variable, it is known an array. We declare an Array Variable with a parenthesis. In the below example, a single-dimension array containing six elements is declared:

Dim myArrayStudents(5)

Here index starts from 0 to 5, so the records will always be one more than the number shown in parenthesis. Value assignments are done in the below way –

myArrayStudents(0) = “Vikram”

myArrayStudents(1) = “Tom”

.

.

myArrayStudents(5) = “Jerry”

Naming Convention:

The standards rule for the naming convention are mentioned below – 

  • It should begin with an alphabetic character.
  • It should not contain an embedded period.
  • Name should be self explanatory.
  • The maximum length should not be exceeds 255 chars.

Constants in UFT:

We can consider a constant as a variable of a number or String whose value will never change. i.e., after defining a constant, it will be acted as a read-only place holder. The VBScript already provides many in build constants variables like , vbOK, vbTrue, vbFalse, vbCancel, and so on. A constant has to be declared and defined using the keyword “CONST” like the below example –

Const MyString = “This is my string.”

Const MyAge = 49

 Within modules, constants are always private, and visibility can not be changed.

Array in UFT:

Array in UFT is a variable that stores a collection of values having the same data type such as number, String, etc. A set of same type elements can be stored in the same memory location where each of the element can be accessed by their reference number which are known as array index. The index of the first element always start from zero value.

While declaring an array we need to define two parts – the logical name of the array and the array subscripts which indicates the highest value of array index. Array indexes are the unique numbers which represent the each element of that array. This index always starts from zero.

It should be declared before use. We can declare an array by the dim statement. Two types of arrays are available in UFT –

·        Local Array – The scope of local array is restricted to the current function or procedure block where it was declared.

·        Global Array – If the array is declared globally or at the start of script, the values of the array (i.e. scope) can be accessed from all the functions or procedures which are defined with in the same vbscript.

Static and Dynamic Arrays:

VBScript allows us to declare static or dynamic array in UFT.

A static array can not be modified during the uft regression. It means, the static array keeps defined number of elements through out the execution.

A dynamic array can be enhanced at any point of time during the test execution.

Declare and assign Values to Arrays:

Declaration statements – Dim myArray(subscriptvalue)

Example: Declaring an array which stores four students name and assigns the value in it.

Dim arrStudents(3)

arrStudents(0) = “Tom”

 arrStudents (1) = “Henry”

arrStudents (2) = “Tina”

arrStudents (3) = “Vicky”

Conditional Statements in UFT:

The conditional statements are most important features of scripting to make it flexible and robust. The mostly used conditional statement in UFT will be explained through out this section.

If-Else Statement:

The If – Else statement is used build the conditional log in the UFT script. The purpose of this statement is to check one or multiple conditions and perform certain activities based on the checking. For an example – Here we will identify the current week day using the if-then-else statement.

If-Else Conditional Statements in UFT
If-Else Conditional Statements in UFT

The multiple elseif statements can be used to check multiple conditions. Also, we can write multiple conditions in the first line of “if statement” which should be separated by logical operators such as OR, NOT, AND, etc.

Select Case Statement / Switch Case Statement:

It is an alternative approach of instead of using multiple if conditions for a single variable. 

The Select-case statement is very helpful for the scenario where based on the different values of a single variable, we need to perform different activities. It’s also know is switch-case-statement. The use of case statement, we can write our script in a organized way. The select case statement execute the conditional expression at the beginning and the result of the conditional expression, will matched with different static conditions in case statement. Here, only the associated steps will be executed for the match case.

For an example – Here we will identify the current week day using the select case statement.

Select - Case Conditional Statements in UFT
Select – Case Conditional Statements in UFT (VB Scripting)

Looping Statements in UFT:

In a situation, where similar codes are executed again and again based on some conditions, it is advisable to use looping statements for better readability and efficiency . In this section, we will discuss about the different kinds of loops with the examples, which are very helpful in UFT scripting. The loops work on conditions or maximum iteration counters. The frequently used looping statements in UFT are explained below –

· Do Loop Statement

· While Loop Satement

· For Loop Statement

Do Loop:

When we need to execute few steps repeatedly based on one or multiple conditions, we need to use the Do Loop statements as it allows to continue the looping based on conditions. The loop will be continued till the conditions are getting satisfied. Here, the conditions are checked at the end of the looping statement. That means, the first iteration is always executed irrespective of the condition checking result. To exit from the middle of the loop for another specific if condition, the “Exit Do” statement can be used. At end of the loop, “while” statement is used for conditional checking. Example – In this example, the loop will be continued, until the counter value reached to 10,

VB Scripting in UFT - Looping Statements in UFT (Do-Loop)
VB Scripting in UFT – Looping Statements in UFT (Do-Loop)

It’s similar to Do-Loop structure. When we need to execute few steps repeatedly based on one or multiple conditions, we can use the While Loop statements as it allows to continue the looping based on conditions. The loop will be continued till the conditions are getting satisfied. Here, the conditions are placed at the beginning of the loop with the help of while keyword. That means, none of the iteration will be executed if the checking of the conditions are failed. To exit from the middle of the loop for another specific if condition, the “Exit While” statement can be used. At beginning of the loop, “while” statement is used for conditional checking. The primary difference between Do-Loop and While-Wend statement is the place of condition checking.

Example – In this example of VB Scripting, the loop will be continued, until the counter value reached to 10,

VB Scripting in UFT -Looping Statements in UFT (While-Wend)
While-Wend Looping Statements in UFT (VB Scripting in UFT)

For Loop:

For– Next statements are defined predefined iteration counter. The loop will be continued till the counter reached the highest value which is specified with “for” keyword. To exit from the middle of the loop for another specific if condition, the “Exit For” statement can be used. At end of the loop, “while” statement is used for conditional checking.

Example – In this example, the loop will be continued for ten iterations,

VB Scripting in UFT - Looping Statements in UFT (For-Next)
VB Scripting in UFT – Looping Statements in UFT (For-Next)

Sub Procedure in UFT:

A sub procedure in UFT is a group of statements which are bounded by the keyword “Sub” and “End Sub” to perform a specific task. It does not return any value. We can pass values to sub procedure as arguments. An empty parenthesis() represents that there is not input arguments. In UFT, the sub is used to create a unit of the functional block for modularization. 

For example, the below code shows that a sub is used to calculate and display the interest value based on the input interest rate, years, and principal amount.

VB Scripting in UFT - Sub Procedure in UFT
VB Scripting in UFT – Sub Procedure in UFT

Function Procedure in UFT:

A function procedure in UFT is a group of statements which are bounded by the keyword “Function” and “End Function” to perform specific activity and return the output. A function procedure in UFT is a block of statements enclosed by Function and End Function statements to perform specific actions. It has the data return capabilities, which is the primary difference with a sub. We can pass values to function procedure as arguments. We need to include an empty parenthesis() when no argument is required. In UFT, Function is used to create a unit of the functional block for modularization. 

For example, the below code shows that a Function is used to calculate and display the interest value based on the input interest rate, years, and principal amount.

VB Scripting in UFT - Function Procedure in UFT
VB Scripting in UFT – Function Procedure in UFT

Conclusion:

In this VB Scripting in UFT article (UFT Tutorial), we have learned about different variables, constants, arrays, conditional and looping statements. Click here to learn more on VB Scripting. Also, if you want to prepare for UFT Interview Questions, please click here.

Leave a Comment