VBScript for Automation (QTP/UFT) Testing – Part 1
In VBScript – Part 1, let’s see the following topics:
Introduction
Prerequisites
General Information
Data Types
Variables
Operators – Arithmetic, Relationship, Logical & Special
Conversion functions
Validation functions
INTRODUCTION:
VBScript (Microsoft Visual Basic Script) is a scripting language developed by Microsoft. VBScript is the scripting language for QTP (Quick Test Professional).
A scripting language is a lightweight programming language
Why should a tester learn VBScript?
If a tester would like to become an Automation (QTP) Tester, learning VBScript is necessary.
PREREQUISITES:
Basic knowledge on computer programming
Tools:
Notepad or Notepad++ or EditPlus. Lots of tools available online for VBScript. Google it and use the one which is handy to you.
GENERAL INFORMATION:
VBScript is case insensitive language (not case sensitive)
VBScript is an interpreter (line by line execution). It is not a compiler (like C++, .net, java)
See the difference between Interpreter and Compiler here.
VBScript is also called as line by line compiler
In windows, 2 Injens to support VB Script file:
wscript.exe (windows script) – default
cscript.exe (command script) – if file is missing/corrupt use this. (cscript “file name”)
Lots of editors are available to write scripts. I suggest Notepad++
DATA TYPES:
VBScript supports only one data type called Variant. The variant data type is a special kind of data type that can contain different kinds of information. It acts as a string when used in a string context and acts as a numeric when used in a numeric context.
VARIABLES:
VBScript variables are used to hold values or expressions
Variable Name Rules:
Must begin with an alphabet
Cannot contain a period (.)
Cannot exceed 255 Characters
Must not use predefined types (e.g. rem, ‘)
Must not use special characters
Must not include space
Variable Declaration:
Variables in VBScript can be declared in three ways:
- Dim Statement
- Public Statement
- Private Statement
Single variable: We declare single variable as shown below
Dim SoftwareTestingMaterial
Multiple variables: We can declare multiple variables by separating variables with comma
Dim SoftwareTestingMaterial, SoftwareTestingMaterial1, SoftwareTestingMaterial2
For a best practice, use Option Explicit statement to declare variables. Sometimes we may misspell the variables names in the script. It leads to failing the script. Option explicit allows us to avoid this issue.
Option Explicit Dim SoftwareTestingMaterial
Assigning a numeric value to a variable:
Option Explicit Dim SoftwareTestingMaterial SoftwareTestingMaterial = 123
In the above expression, SoftwareTestingMaterial is a variable and 123 is a value
Assigning a string value to a variable:
Option Explicit Dim SoftwareTestingMaterial SoftwareTestingMaterial = "stm"
In the above expression, SoftwareTestingMaterial is a variable and “stm” is a string value
Assigning a number as a string to a variable:
Option Explicit Dim SoftwareTestingMaterial SoftwareTestingMaterial = "123"
Assigning multiple values to a variable:
Option Explicit 'Dim arrayvalue(size) Dim SoftwareTestingMaterial(2)
Note: Refer Arrays.
SUB DATA TYPES:
TypeName()
It gives the sub-data type of a variable
Example:
var="Software Testing Material" msgbox typename(var)
Different Sub Data Types available in VBScript:
Integer, String, Double, Date, Currency, Boolean, Empty, Null, Error, Nothing
OPERATORS:
Different Operators supported in VBScript:
- Arithmetic Operators
- Relationship Operators
- Logical Operators
- Special Operators
ARITHMETIC OPERATORS:
+, -, *, /, \
Example:
Dim a,b a=20: b=10 msgbox a+b msgbox a-b msgbox a*b msgbox a/b msgbox a\b
RELATIONSHIP OPERATORS:
Always returns the Boolean Value
=, >, <, >=, <=, <>
Example:
Option explicit Dim a,b a=5: b=4 msgbox a=b msgbox a<b msgbox a>b msgbox a>=b msgbox a<=b msgbox a<>b
LOGICAL OPERATORS:
Accepts the input as Boolean value
AND, OR, NOT
A | B | Series | Parallel | Reverse |
A AND B | A OR B | NOT A | ||
T | T | T | T | F |
T | F | F | T | F |
F | T | F | T | T |
F | F | F | F | T |
SPECIAL OPERATORS:
Single Quote (') - To comment
Example:
Str="Software Testing Material" 'msgbox str
Rem - To comment
Example:
str="Software Testing Material" rem msgbox str & - concatenation
Example:
str="Software Testing Material" str1="website: " msgbox str1 & str
= – Assignment
Example:
option explicit Dim a,b,c a=5: b=4: c=3 a=b=c msgbox a
Example:
Option explicit Dim a,b,c,d a=5: b=4: c=3: d=false a=b=c=d msgbox a
Note: The first left most “=” operator is an assignment operator and remaining all are relationship operators.
INPUT BOX FUNCTION:
str = Inputbox("Enter the value") msgbox str msgbox typename(str)
CONVERSION FUNCTIONS:
It converts from one valid sub-data type to another valid sub-data type.
cint() – Converts from String (it should be always a numeric string) to a variant of subtype Integer.
cdbl() – Converts from String to a variant of subtype double value
cdate() – Converts a valid date and time expression to the variant of subtype Date
cbool() – Converts from String to a variant of subtype Boolean value string either True/False
cstr() – Converts from Integer to a variant of subtype String
ccur() – Converts an expression to a variant of subtype Currency
cbyte() – Converts an expression to a variant of subtype Byte
Example:
str = "123" msgbox cint(str) msgbox typename(str) msgbox typename(cint(str))
VALIDATION FUNCTIONS:
To validate the type of the data
All the validation functions returns Boolean value
Some of the important validation functions are
Isnumeric(), Isempty(), Isarray(), Isobject()
Example:
option explicit dim a, b, res a = inputbox("Enter the value") b = inputbox ("Enter the value") res=a+b msgbox res
Input: 4 and 5
Output: 45
If isnumeric(a) and isnumeric(b) then res = cint(a) + cint(b) msgbox res else msgbox "Enter integer value" End if
I would like to conclude VBScript – Part 1 here and will start VBScript – Part 2 in the next post.
VBScript for Automation (QTP/UFT) Testing – Part 1
VBScript for Automation (QTP/UFT) Testing – Part 2
VBScript for Automation (QTP/UFT) Testing – Part 3
VBScript for Automation (QTP/UFT) Testing – Part 4
VBScript for Automation (QTP/UFT) Testing – Part 5
Excellent post!!! QTP has been a leading automation tool. Thanks admin for sharing this useful information with us. Keep on updating.
sir how is the output 45 in isnumeric program??
Hi Manjit, we are passing two input string values 4 and 5 so the output is 4+5 (4 concatenate 5) = 45. If you convert the input value to integer (cint) then it will be 9 (cint(4)+cint(5).
Here is the sample,
option explicit
dim a, b, res
a = inputbox(“Enter the value”)
b = inputbox (“Enter the value”)
res=cint(a)+cint(b)
msgbox res