Introduction of VB script
VBScript Introduction
Microsoft VBScript is a general-purpose, lightweight scripting language
developed by Microsoft that is inherited from Visual Basic. Nowadays, VBScript is
the primary scripting language for Unified Functional Tool (UFT), which is a
test automation tool.
This blog will teach you how to
use VbScript scripting language for UFT scripting and general purposes.
This blog has
been prepared for the beginners or intermediate to help them understand basic-to-advanced
functionality of VBScript. After completing, you will find yourself at next level of understanding about VB script.
Here we are travel throughout the vbscript usage,In built functions, Methods etc.
VBScript
stands for Visual Basic Scripting that forms a
subset of Visual Basic for Applications (VBA).
Features of VBScript
·
VBScript is a lightweight scripting
language, which has a lightning fast interpreter.
·
VBScript, for the most part, is case
insensitive. It has a very simple syntax, easy to learn and to implement.
·
Unlike C++ or Java, VBScript is an
object-based scripting language and NOT an Object-Oriented Programming
language.
·
It uses Component Object Model (COM) in
order to access the elements of the environment in which it is executing.
·
Successful execution of VBScript can
happen only if it is executed in Host Environment such as Internet
Explorer (IE), Internet Information Services (IIS) and
Windows Scripting Host (WSH)
VbScript – Version History and Uses
VBScript was introduced by
Microsoft way back in 1996 and the first version was 1.0. The Current Stable
version of VBScript is 5.8, which is available as part of IE8 or Windows 7.
The VBScript usage areas are
aplenty and not restricted to the below list.
·
VBScript is used as a scripting language in one of the popular
Automation testing tools – Unified Functional Tool Test Professional abbreviated as UFT.
·
Windows Scripting Host, which is used mostly by Windows System
administrators for automating the Windows Desktop.
Also it has some disadvantageous mentioned as below
Disadvantages
·
VBscript is used only by IE Browsers. Other browsers such as
Chrome, Firefox DONOT Support VBScript. Hence, JavaScript is preferred over
VBScript.
·
VBScript has a Limited command line support.
·
Since there is no development environment available by default,
debugging is difficult.
Whitespace and Line Breaks
VBScript ignores spaces, tabs and
newlines that appear within VBScript programs.
Because one can use spaces, tabs
and newlines freely within the program so you are free to format and indent
your programs that makes the code easy to read and
understand and no statement terminator like semicolon.
Single Line Syntax
Colons are used when two or more
lines of VBScript ought to be written in a single line. Hence, in VBScript,
Colons act as a line separator.
var1 = 10 : var2 = 20 var3 = var1 + var2 : var3 = var3 + 2
Multiple Line Syntax
When a statement in VBScript is
lengthy and if user wishes to break it into multiple lines, then the user has
to use underscore "_".
This improves the readability of
the code. The following example illustrates how to work with multiple lines.
var1 = 10
var2 = 20
var3 = var1 + var2
Reserved Words
The following list shows the
reserved words in vb Scripting. These reserved words should not be used as a
constant or variable or any other identifier names.
Loop
|
LSet
|
Me
|
Mod
|
New
|
Next
|
Not
|
Nothing
|
Null
|
On
|
Option
|
Optional
|
Or
|
ParamArray
|
Preserve
|
Private
|
Public
|
RaiseEvent
|
ReDim
|
Rem
|
Resume
|
RSet
|
Select
|
Set
|
Shared
|
Single
|
Static
|
Stop
|
Sub
|
Then
|
To
|
True
|
Type
|
And
|
As
|
Boolean
|
ByRef
|
Byte
|
ByVal
|
Call
|
Case
|
Class
|
Const
|
Currency
|
Debug
|
Dim
|
Do
|
Double
|
Each
|
Else
|
ElseIf
|
Empty
|
End
|
EndIf
|
Enum
|
Eqv
|
Event
|
Exit
|
False
|
For
|
Function
|
Get
|
GoTo
|
If
|
Imp
|
Implements
|
In
|
Integer
|
Is
|
Let
|
Like
|
Long
|
TypeOf
|
Until
|
Variant
|
Wend
|
While
|
With
|
Xor
|
Eval
|
Execute
|
Msgbox
|
Erase
|
ExecuteGlobal
|
Option Explicit
|
Randomize
|
SendKeys
|
Case Sensitivity
VBScript is a case-insensitive
language. This means that language keywords, variables, function names and any
other identifiers need NOT be typed with a consistent capitalization of
letters. So identifiers int_counter,
INT_Counter and INT_COUNTER have the same meaning within VBScript.
VBScript in Internet Explorer
Here are simple steps to turn on
or turn off VBScript in your Internet Explorer:
·
Follow Tools->
Internet Options from the
menu
·
Select Security tab from the dialog box
·
Click the Custom
Level button
·
Scroll down till you find Scripting
option
·
Select Enable radio button under Active scripting
·
Finally click OK and come out
To disable VBScript support in
your Internet Explorer, you need to select Disable
radio button under Active
scripting.
VBScript Variables
Variable is a named memory
location used to hold a value that can be changed during the script execution.
VBScript has only ONE fundamental data type,Variant.
Rules for Declaring Variables:
·
Variable Name must begin with an alphabet.
·
Variable names cannot exceed 255 characters.
·
Variables Should NOT contain a period (.)
·
Variable Names should be unique in the declared context.
Declaring Variables
Variables are declared using
“dim” keyword. Since there is only ONE fundamental data type, all the declared
variables are variant by default. Hence, a user NEED NOT mention
the type of data during declaration.
Example 1: In this Example,
IntValue can be used as a String, Integer or even arrays.
Dim Var1
Example 2: Two or more
declarations are separated by comma (,)
Dim Variable1, Variable2
Assigning Values to the Variables
Values are assigned similar to an
algebraic expression. The variable name on the left hand side followed by an
equal to (=) symbol and then its value on the right hand side.
Rules:
·
The numeric values should be declared without double quotes.
·
The String values should be enclosed within doublequotes(")
·
Date and Time variables should be enclosed within hash symbol(#)
Examples:
Dim StrValue
StrValue = “VBScript”
Scope of the Variables
Variables can be declared using
the following statements that determines the scope of the variable. The scope
of the variable plays a crucial role when used within a procedure or classes.
- Dim
- Public
- Private
Dim
Variables declared using “Dim” keyword
at a Procedure level are available only within the same procedure. Variables
declared using “Dim” Keyword at script level are available to all the
procedures within the same script.
Public
Variables declared using
"Public" Keyword are available to all the procedures across all the
associated scripts. When declaring a variable of type "public", Dim
keyword is replaced by "Public".
Example : In the below example,
Var1 and Var2 are available at script level while Var3 is available across the
associated scripts and procedures as it is declared as Public.
Private
Variables that are declared as
"Private" have scope only within that script in which they are
declared. When declaring a variable of type "Private", Dim keyword is
replaced by "Private".
Declaring Constants
Constant is a named
memory location used to hold a value that CANNOT be changed during the script
execution. If a user tries to change a Constant Value, the Script execution
ends up with an error. Constants are declared the same way the variables are
declared.
[Public | Private] Const Constant_Name = Value
The Constant can be
of type Public or Private. The Use of Public or Private is Optional. The Public
constants are available for all the scripts and procedures while the Private
Constants are available within the procedure or Class. One can assign any value
such as number, String or Date to the declared Constant.
What is an operator?
Simple answer can be
given using expression 4
+ 5 is equal to 9. Here, 4 and 5 are called operands and + is called operator.
VBScript language supports following types of operators:
Arithmetic Operators
Comparison Operators
Logical (or
Relational) Operators
Concatenation
Operators
The Arithmetic Operators
There are following
arithmetic operators supported by VBScript language:
Assume variable A
holds 5 and variable B holds 10, then:
Operator
|
Description
|
+
|
Adds two operands
|
-
|
Subtracts second
operand from the first
|
*
|
Multiply both
operands
|
/
|
Divide numerator by
denumerator
|
%
|
Modulus Operator
and remainder of after an integer division
|
^
|
Exponentiation
Operator
|
The Comparison Operators
There are following
comparison operators supported by VBScript language:
Assume variable A
holds 10 and variable B holds 20, then:
Operator
|
Description
|
==
|
Checks if the value
of two operands are equal or not, if yes then condition becomes true.
|
<>
|
Checks if the value
of two operands are equal or not, if values are not equal then condition
becomes true.
|
>
|
Checks if the value
of left operand is greater than the value of right operand, if yes then
condition becomes true.
|
<
|
Checks if the value
of left operand is less than the value of right operand, if yes then
condition becomes true.
|
>=
|
Checks if the value
of left operand is greater than or equal to the value of right operand, if yes
then condition becomes true.
|
<=
|
Checks if the value
of left operand is less than or equal to the value of right operand, if yes
then condition becomes true.
|
The Logical Operators
There are following
logical operators supported by VBScript language:
Assume variable A
holds 10 and variable B holds 0, then:
Operator
|
Description
|
AND
|
Called Logical AND
operator. If both the conditions are True then Expression becomes true.
|
OR
|
Called Logical OR
Operator. If any of the two conditions are True then condition becomes true.
|
NOT
|
Called Logical NOT
Operator. Use to reverses the logical state of its operand. If a condition is
true then Logical NOT operator will make false.
|
XOR
|
Called Logical
Exclusion. It is the combination of NOT and OR Operator. If one, and only
one, of the expressions evaluates to True, result is True.
|
hope you have enjoyed learning, happy learning !
if you have any query please pass the comment below...
Comments
Post a Comment