By Anish Pillai Anish Pillai Posted under qtp and vbscript tutorials | QTP Tutorials for Beginners

Part 1: QTP and VBScript | Introduction to VBScript

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×

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.

QTP and 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.
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.

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×
  • Pingback: Part 2: QTP and VBScript | Operators in VBScript - Automation Repository - Automation Repository()

  • Shilpi

    Thanks a lot! Amazingly precise and clear tutorial!!

    • Anish10110

      Thank you Shilpi.. 🙂

  • ram

    Hi Anish,

    i really impressed with your blog.

    u r great…..

    • Anish10110

      Thank You Ram.. 🙂

  • kal

    Thank you very much for an amazing tutorials.

    • Anish10110


  • Ajay

    I am new to QTP and the blog is simply awesome . Just have a question on this line
    'c = 30 — This statement would throw an error
    Since this starts with a single quote wouldnt this be assumed a comment and be ignored .
    Why would it throw an error.

  • Anish10110

    Hi Ajay,

    Yes, you mentioned it correctly, Because of the single quote, there would be no error. But if you remove it the error will be there. The statement was put there (with single quote) just for information purpose..

  • praveena

    This is really a good website for the beginners who wants to learn QTP from basics. I have been learning QTP from one month in online but I couldn't find any tutorial in such detail like this. Thank you

    • Anish10110

      Thank you Praveena.. 🙂

  • laxmi

    nice presentations !, really good one to follow.

    • Anish10110

      Thank You.. 🙂

  • pawan

    Hi Anish

    Thanks a lot for U and this great Website on QTP.

    i have undergone training in QTP for two times, but i didnt get any chance to work on automation and still stuck up in manual testing for past 4 years.

    ur blog is keeping me in touch with QTP concepts which are vanishing form my brain.

    please help if i post any question. Thanks a lot..Great work.

    helps many people like me.

  • kiranmayi

    It is like a class room notes. It is very helpful.Thanks alot.

  • archana

    very much informative 🙂

  • Prasaad

    Really.. Superb.. Anish .. Thanks a lot for much informative..!! 🙂

    • Anish10110

      Thank you Prasaad.. 🙂

  • aditya

    really good work!!! Thanks.

  • subbu

    good for VB script experts

  • Virugai Mohan Kumar

    In the "Variable Declaration and Assignment" block , why is the startdate value and starttime value is given within # ?? Is using # equals to double quotes in VBScript?

    • MikeS

      Virugai, #<text># is recognized by VBScript as a date or time declaration

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×