No Recording, Pure Scripting ! Learn the VB way.

VB Script Introduction

Do you wanna automate test cases without any record and playback ? Yes. Then you need to learn ‘VBscript’ which is very easy. Let us look at the basics of VBScript in this post.

What is VBScript ?

‘VB script’ is a light weight scripting language provided by Microsoft and default language used in UFT / QTP. VBScript is not case sensitive.

VBScript

VBScript Variables :

A variable is a memory location to store values. The values can change during execution.

A=100 where ‘A’ is a variable which holds a value 100.
var1=500 , City="Paris" etc 

VBScript Constants :

Constants hold values that cannot change during program execution. Using constants makes your code more maintainable. Constants are already defined in VBScript, you don’t need to explicitly declare them in your code. Simply use them in place of the values they represent. There is an option to declare user defined constants also.

vbBlack for Black (Color Constant)
vbTab for Horizontal Tab (string Constant)
Const Pi=3.14 (user defined)

Types of variables in VB Script :

  • Explicit Variables
  • Implicit Variables

Explicit Variables :

Variables are declared before using them in the script are called as explicit variables. Using Dim, Public and Private statements variables are declared.

Dim Statement :
    Syntax: Dim Var1, Var2, Var 3
    Example: Dim iphone, macbook , price.
    Variables declared with Dim at the script level are available to all procedures within the script.
Private Statement:
    Syntax:Private Var4, Var5, Var6
    Example:Private apple, Samsung, Nokia
    Private statement variables are available only to the script in which they are declared.
Public Statement:
    Syntax: Public Var7, Var8, Var9
    Example: Public Xbox, Chrome book, Firebug
    Public statement variables which are also available to all procedures in all scripts.

VB Script Variables

Implicit Variables :

If a variable is used without declaring it, then VB script automatically declares the variables. These are known as Implicit variables. Implicit variable declaration is not recommended because, a spelling mistake is also considered as a new variable which result in errors.

For e.g., FoodWorldnbr=25 at one place and some other has FoodWrldnbr=40.

To avoid this problem ‘Option Explicit’ statement is used.

Option Explicit :

Option Explicit statement restricts the usage of new variables in the script without declaration. If used, the Option Explicit statement must appear in a script before any other statements. When Option Explicit statement is used, one must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an ‘undeclared variable’ then it results in error.

Example 1: 
Dim var1, var2 
var1 = 10
var2 = 20
Total = var1 + var2 'Total is Implicit variable
Msgbox Result 
Output: 30

Example 2:
Option Explicit
Dim var1, var2 
var1 = 10
var2 = 20
Result = var1 + var2 
Msgbox Result  'Error will be displayed. 

For Option Explicit,variable declaration is mandatory.

VB Script Data Type :

  • VB Script has only one data type called a Variant.
  • A Variant can contain either numeric or string information.
  • It behaves as a number when you use it in a numeric context and as a string when you use it in a string context.

Comments in VB Script:

Comments are non executable statements. A single quotation mark (‘) is used to represent comments.
Rem Statement:To make a particular line / complete code as a comment use Rem statement.

Comments in VBscript and UFT QTP

Variable Naming Rules :

  • Must begin with a letter or the underscore
  • Cannot contain special characters except the underscore
  • Cannot match a reserved word
  • Must be unique within a context
Examples:
Right: name, _age
Wrong: 1name, #age, for, if

This is an introduction to vbscript, There will be series of posts for vbscript, please do check in the ‘Vbscript’ category for all the respective posts.