Basics of vbscript

VBscript is the scripting language used in QTP. It is developed by Microsoft. VBscript is subset of VB (Visual Basic) and VBA(Visual Basic of Applications).

VBscript is used by other technologies also. For example, it is used in ASP (Active Server Page) for web site development. So we will be getting more ready-made functions/code written in vbscript from the Internet. It will save QTP script development time.

VBscript will access the host/system thro’ Microsoft’s Windows Script Host (WSH). We can use WSH scripts also in QTP. It can be effectively used to automate the Test scenarios such as rebooting the system automatically after doing some steps and locking the system automatically.

QTP recording feature will automatically generate VBscript code while recording the steps.

And, QTP IDE is having ‘Function Generator” for creating the vbscript functions.

VBScript Variables

In VBScript, all variables are of type variant, that can store different types of data.
Rules for VBScript variable names:

  • Must begin with a letter
  • Cannot contain a period (.)
  • Cannot exceed 255 characters

dim will be used for declaring the variable as below.
Dim TestCaseID

The value for this variable can be assigned as below
TestCaseID=”TC1″

Remember to use option explicit at top of your script. Otherwise a new variable will be created automatically if you misspell the variable name when assigning value for it.

We need to understand scope/lifetime of variable clearly. A variable declared within a function will exist only within that function. That means the variable will be destroyed when exiting the function, and more than one function can have variable with same name. So it is called as Local variable.

So, it is very important to have clear understanding about the scope/lifetime of variable declared/used in Test/Action/function library/datatable/environment.

Array variable can be declared as below.
Dim ArrIDs(10)
The above declaration will create single-dimension array containing 11 elements. i-e the array in vbscript is 0 based.

Operators

Arithmetic

Description

Symbol

Exponentiation ^
Unary negation
Multiplication *
Division /
Integer division \
Modulus arithmetic Mod
Addition +
Subtraction
String concatenation &

Comparison

Description

Symbol

Equality =
Inequality <>
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Object equivalence Is

Logical

Description

Symbol

Logical negation Not
Logical conjunction And
Logical disjunction Or
Logical exclusion Xor
Logical equivalence Eqv
Logical implication Imp

 

VBScript Procedures

 

In VBScript, there are two types of procedures:

  • Sub procedure
  • Function procedure

A Sub procedure:

  • is a series of statements, enclosed by the Sub and End Sub statements
  • does not return a value
  • can take arguments
  • without arguments, it must include an empty set of parentheses ()

eg.
Sub displayName()
msgbox(“QualityPoint Technologies”)
End Sub

or

Sub addvalues(value1,value2)
msgbox(value1+value2)
End Sub

When calling a Sub procedure you can use the Call statement, like this:

Call MyProc(argument)

Or, you can omit the Call statement, like this:

MyProc argument

A Function procedure:

  • is a series of statements, enclosed by the Function and End Function statements
  • can return a value
  • can take arguments
  • without arguments, must include an empty set of parentheses ()
  • returns a value by assigning a value to its name

Find below a Sample function.

Function addvalues(value1,value2)
addvalues=value1+value2
End Function

The above function will take two arguments and will add those two values and then it will return the sum value. Note here the sum value is retured by assigning it to the function name.

The above function can be called as below.

msgbox “Sum value is ” & addvalues(1,2)

Conditional Statements

 

In VBScript we have four conditional statements:

if statement – executes a set of code when a condition is true

(e.g) if i=10 then
msgbox “I am 10”
End if
if…then…else statement – select one of two sets of lines to execute
(e.g) if i=10 then  msgbox “I am 10”
else msgbox “other than 10”
end if
if…then…elseif statement – select one of many sets of lines to execute

(e.g) if i=10 then  msgbox “I am 10”
elseif i=11 then msgbox “I am 11”
else msgbox “unknown”
end if

select case statement – select one of many sets of lines to execute

select case value
case 1
msgbox “1”
case 2
msgbox “2”
case 3
msgbox “3”
case else
msgbox “other than 1,2 and 3”
end select

Looping Statements

Use the For…Next statement to run a block of code a specified number of times.
e.g
for i = 0 to 5
msgbox(“The number is ” & i )
next

If you don’t know how many repetitions you want, use a Do…Loop statement.

The Do…Loop statement repeats a block of code while a condition is true, or until a condition becomes true.

Built-in Functions

VBscript is having many useful built-in functions.
You can refer this page for the complete list
inStr, isNull, LCase, Left, Len, Mid, Now, Replace, Split, UBound, CStr, CreateObject, Date and DatePart are functions that are most frequently used in QTP script development.