Monday 12 August 2013

Introduction to VBScript

VBScript – Quick Introduction
VBScript, modeled on Visual Basic, is an active scripting language developed by Microsoft. VBScript is a light programming language that uses the Component Object Model to access elements of the environment within which it is running. The first version of VBScript, version 1.0, was released by Microsoft in 1996. The current stable version of VBScript that is available is version 5.8. QTP uses VBScript to specify a test procedure, and to manipulate the objects and controls of the application under test.
Data Types in VBScript
VBScript has only one data type known as a Variant. A Variant is a special kind of data type that can contain different types of values, based on how it is used. The Variant data type in VBScript has different subtypes that represent different types of values stored in a Variant data type. Some of the common Variant subtypes that are generally used in QTP are listed in the below table.

Subtype
Description
Integer
Contains an integer value
String
Contains a text
Boolean
Contains either True or False
Date
Contains a value that represents a date.
Object
Contains an object

Variables in VBScript
Variable Declaration and Assignment

You can explicitly declare variables in QTP using Dim, Public or Private Statement (Dim is the most commonly used option).

Assigning a value to a variable follows the standard procedure where the variable is in the left hand side followed by equal to operator with the value on the right hand side.
Note:

1) It is not necessary to declare a variable to use it.

2) You cannot declare a variable and assign value to it in the same statement.

3) You can use the same variable for storing multiple types of values such as a string, integer etc (although this is not considered a good programming practice)

4) You can write multiple statements in the same line in QTP by separating them using a colon (:)

5) Comments can be specified in QTP using single quote (‘) or Rem statement followed by the text.

Let’s see some examples for variable declaration & assignment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'Declaring variables
Dim a
Dim b
Dim c, d, e 'Multiple Declarations in a single statement

'Assigning values
a = 1
b = "some text"
c = 2 : d = "abc" : e = true 'Multiple statements in single line

'Assigning value to undeclared variable
startDate = #06/18/2008#
startTime = #3:36:00 PM#

'Dim f = 12 -- This statement would give error..

'Assigning different types of data to same variable
a = 1
a = "text"
a = false

'This is a comment
Rem This is also a comment

Using Option Explicit in QTP. 

Using Option Explicit statement in QTP forces you to declare every variable that you want to use in your script. Using any undeclared variable will result in an error.

1
2
3
4
5
6
7
Option Explicit

Dim a, b

a = 10
b = 20
'c = 30 -- This statement would throw an error

Scope of variables in VBScript. 

A variable’s scope is determined by where it is declared. If you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.

The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed.
Arrays in VBScript
1)   Array variables are declared the same way as normal variables except that the variable name should be followed by parentheses.

2) In a two-dimensional array, the first number represents the number of rows and the second number represents the number of columns.

3) The size of an array can be changed at run-time using ReDim statement.

4) You can also use the Preserve keyword to preserve the contents of the array during its re-size.

Let’s see some examples of how to use arrays in QTP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'Declare an array of size 10
Dim arr(9)

arr(0) = "0"
arr(1) = "1"
'.. and so on..

'Declare Multi-Dimension array
Dim arrM(1, 1)
arrM(0,0) = "00"
arrM(0,1) = "01"
arrM(1,0) = "10"
arrM(1,1) = "11"

'Using Dynamic Array
Dim arr_New()
'.......
'.......

ReDim Preserve arr_New(1)
arr_New(0) = "0"
arr_New(1) = "1"

ReDim Preserve arr_New(3)
arr_New(2) = "2"
arr_New(3) = "3"

This was all about how you can use variables in QTP.