VB script Tutorial Part 1

Explanation of VBScript for use in HP Unified functional Tool (UFT)

Index -:
1. VBscript in QTP / UFT
2. VB script Datatype and sub types
3. Arrays in VBscript

1.     VBScript in QTP / UFT 
VBScript (short form of Visual Basic Scripting Edition) is a lively scripting language interpreted via Microsoft's Windows Script Host.

It is a light scripting language and primary used language for UFT and no need to install externally it is available by default in windows environment.

VBScript has many powerful functions and provides excellent support for variables, data types, and error handling.

Two script engines can interpret VBScript- VBScript.dll, which is invoked by asp.dll is used in web environment and Wscript.exe & Cscript.exe in Windows GUI environment using Windows Script Host (WSH). 

We typically, use VBScript within WSH to automate systems administration tasks. WSH is the system module that transforms a VBScript file into a Windows executable file. 
Wscript.exe is used to display output and receive input in Windows GUI format such as dialog and input boxes.

Cscript.exe is used in a command-line environment. When VBScript source code is contained in standalone files, they have the file extension .Vbs.

2.     VBScript Data Type 

VBScript is a typeless language. This means that variables cannot be restricted to a single datatype.

VBScript does not allow you to specify in advance that a particular variable can hold only a particular kind of data.

Instead, VBScript uses a single kind of variable, known as a variant, which can store any kind of data.

A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used.

Suppose if you’re using a variable for assigning a numeric value, that variable behaves like a numeric data type. If you assign string value, that variable behaves like a string.
Scripting languages appear typeless only to the script editor. Internally scripting languages work with datatypes.

For example, when presented with a simple statement such as c = a + b, the scripting language must derive typed values for both a and b.

Those two variants and create typed values such as integer or string. After it has derived the typed values, the scripting language can then perform the operation.
Example -:
iFirstNumber = InputBox(“Enter first number”)
iSecondNumber = InputBox(“Enter Second number”)
iTotal = iFirstNumber + iSecondNumber

Expected output is addition of two numbers but it will concatenate the numbers, because it takes input as string.

VBScript contains following sub datatypes in the Variant.
Empty
Null
Boolean
Byte
Integer
Currency
Long
Single
Double
Date (Time)
String
Object
Error
VBScript datatype can be converted using conversion function mentioned as below

CBool - Converts any nonzero value to True and 0 (zero) to False.
CByte - Converts an expression to a Byte value.
CCur - Converts an expression to a Currency value.
CDate - Converts an expression to a Date value.
CDbl - Converts an expression to a Double value.
CInt - Converts an expression to an Integer value. If the fractional part of the expression is .5, CInt will round the value to the nearest even number. For example, 3.5 will be rounded to 4, and 6.5 will be rounded to 6.
CLng - Converts an expression to a Long value.
CSng - Converts an expression to a Single value.
CStr - Converts an expression to a String value. 

Working with Empty Variables and Null Variables
Understanding the difference between an Empty variable and a Null variable can make an equally big difference in the success or failure of your scripts. An Empty variable is a variable that has not been initialized.

For example, in a statement such as Dim strTextString, the variable is considered "empty" until you set strTextString equal to a value.

By contrast, a Null variable is a variable that has not had a valid value assigned to it.


1.     VBScript Arrays

A variable containing a single value is a scalar variable. We can create a variable that can contain a series of values using an index number. This is called an array variable. Arrays are useful when we are storing sets of similar data. We can store any kind of data in an array. The array can hold a combination of data types.
There are two types of Array available in vb script.

1.    Fixed Length array or static array - : static arrays have a specific number of elements in them but it force to declare size of array. Every element of an array is associated with a unique index number and index start with 0.
For Example – Dim myArray (5), here array size is 5 elements and it would contain 6 elements because it starts with 0.

2.    Dynamic array or run time array -: A dynamic array is created in the same way as a fixed array, but we don't put any size in the declaration. It allow to decide size of array at run time. The benefit of a dynamic array is that if we don't know how large the array will be when we write the code.

For example – Dim myArray (), declare array variable with no size with ReDim we reset the upper bound to a new value. For increase size of array or decide size of array we can use optional Preserve keyword that states that all of the old elements must be preserved when changing the size of array.

The initially we can declare array using Dim keyword or ReDim keyword
In following example we can resize array number times
ReDim myArray (25) and then ReDim myArray (30).

Creating dynamic array example



Dim arrTestArray()
intSize = 0
ReDim Preserve arrTestArray(intSize)
arrTestArray(intSize) = "A"
ReDim Preserve arrTestArray(intSize + 1)
arrTestArray(intSize + 1) = "B"



Array keywords and usage -:

Keyword
Functionality or usage
Dim
It Declare an array
Erase
Reinitializes the elements if it is a static size array and deallocates the memory used if it is a dynamic array
IsArray
This will Return True if A is an array, False if it is not
LBound
This will Return lower bound of an array, in VBScript it will always return 0
Preserve
Preserve (Optional) is used to preserve the data in an existing array, when you resize it.
ReDim
This is used to size or resize a dynamic array.
UBound
This will Return all upper- bound of array or Maximum size of array

Comments

Post a Comment

Popular posts from this blog

Excel Operation in VBScript

Usage of File system object (FSO) in Vbscript. QTP / UFT

How to set browser language for chrome for localization testing