Part 1: QTP and VBScript | Introduction to VBScript
With this post, we are starting off a series of articles on the basic concepts of VBScript that you would be using extensively while creating automation test scripts in QTP. These articles would not be providing a comprehensive reference of VBScript. Instead, this would act as a quick refresher course on VBScript that would help you get started with QTP.
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 declaration & assignment.
'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.
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.
'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. In the upcoming articles, we’ll be discuss more about various other VBScript concepts. Please use the comments section or the contact page to connect with us in case you have any queries about the concepts mentioned in this article. Thanks for visiting.. :->
If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.
Pingback: Part 2: QTP and VBScript | Operators in VBScript - Automation Repository - Automation Repository()