VBScript for Automation (QTP/UFT) Testing – Part 1

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:

  1. Dim Statement
  2. Public Statement
  3. Private Statement

Single variable: We declare single variable as shown below

1
Dim SoftwareTestingMaterial

Multiple variables: We can declare multiple variables by separating variables with comma

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

1
2
Option Explicit
Dim SoftwareTestingMaterial

Assigning a numeric value to a variable:

1
2
3
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:

1
2
3
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:

1
2
3
Option Explicit
Dim SoftwareTestingMaterial
SoftwareTestingMaterial  = “123”

Assigning multiple values to a variable:

1
2
3
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:
1
2
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:

  1. Arithmetic Operators
  2. Relationship Operators
  3. Logical Operators
  4. Special Operators

ARITHMETIC OPERATORS:

1
+, , *, /, \
Example:
1
2
3
4
5
6
7
Dim a,b
a=20: b=10
msgbox a+b
msgbox ab
msgbox a*b
msgbox a/b
msgbox a\b

RELATIONSHIP OPERATORS:

Always returns the Boolean Value

1
=, >, <, >=, <=, <>
Example:
1
2
3
4
5
6
7
8
9
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:

1
Single Quote () To comment
Example:
1
2
Str=“Software Testing Material”
msgbox str
1
Rem To comment
Example:
1
2
3
4
str=“Software Testing Material”
rem msgbox str
 
&   concatenation
Example:
1
2
3
str=“Software Testing Material”
str1=“website: “
msgbox str1 & str

= – Assignment

Example:
1
2
3
4
5
option explicit
Dim a,b,c
a=5: b=4: c=3
a=b=c
msgbox a
Example:
1
2
3
4
5
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:

1
2
3
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:
1
2
3
4
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:
1
2
3
4
5
6
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

1
2
3
4
5
6
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 Series:

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

Back To Top