VB Script 01
VB Script 01
Session 1
Dani Vainstein
Dani Vainstein
What is VBScript?
A Safe subset of visual basic
The VBScript Code is case Insensitive.
Microsoft Visual Basic Scripting Edition brings
active scripting to a wide variety of environments,
including Web client scripting in Microsoft Internet
Explorer and Web server scripting in Microsoft
Internet Information Service (IIS).
Dani Vainstein
Version
1.0
Internet
Explorer 3.0
8/96
Version
2.0
IIS 2
12/96
Version
3.0
Version
3.1
Internet
Windows
Explorer 4.0
98
8/97
Version
4.0
Visual
Studio 6
Version
5.0
Internet
Explorer 5.0
Win NT 5.0
Note:
Note:QuickTest
QuickTestuses
usesVBScript
VBScript5.6
5.6
Dani Vainstein
Visual Basic
Best tool for distributed client/Server solutions.
Powerful, robust, scalable.
Dani Vainstein
Dani Vainstein
Dani Vainstein
Description
Empty
Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string
variables.
Null
Boolean
Byte
Integer
Currency
-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long
Single
Double
Date
Contains a number that represents a date between January 1, 100 to December 31, 9999.
String
Object
Contains an object.
Error
Dani Vainstein
Variables
A variable is a convenient placeholder that refers to
a computer memory location where you can store
program information that may change during the
time your script is running.
For example, you might create a variable called
ClickCount to store the number of times a user
clicks an object on a particular Web page.
you only have to refer to a variable by his name to
see or change its value.
Dani Vainstein
Declaring Variables
You declare variables explicitly in your script using the
Dim statement, the Public statement, and the Private
statement. For example:
Dim DegreesFahrenheit
You declare multiple variables by separating each
variable name with a comma. For example:
Dim Top, Bottom, Left, Right
Dani Vainstein
10
Declaring Variables
You can also declare a variable implicitly by
simply using its name in your script.
That is not generally a good practice because
you could misspell the variable name in one
or more places, causing unexpected results
when your script is run.
For that reason, the Option Explicit
statement is available to require explicit
declaration of all variables.
Dani Vainstein
11
12
Naming Restrictions
Must begin with an alphabetic character.
Cannot contain an embedded period.
Must not exceed 255 characters.
Must be unique in the scope in which it is
declared.
Tip
Tip meaningfull
meaningfullprefix
prefixto
tovariables
variablesto
toindicate
indicatethe
the
subtypes
subtypesi.e
i.e
iCounter
iCounter(integer),
(integer),strName
strName(String),
(String),bFlag
bFlag
(Boolean),
(Boolean),dteToday
dteToday(Date)
(Date)
Dani Vainstein
13
VBScript Keywords
Empty
The Empty keyword is used to indicate an uninitialized variable
value.
Null
The Null keyword is used to indicate that a variable contains no
valid data.
True
The True keyword has a value equal to -1.
False
The False keyword has a value equal to 0.
Nothing
The Nothing keyword in VBScript is used to disassociate an object
variable from any actual object.
Dani Vainstein
14
Dani Vainstein
15
Dim Statement
Dim varname[([subscripts])][, varname[([subscripts])]] . . .
Declares variables and allocates storage space.
Variables declared with Dim at the script level are available to all
procedures within the script.
At the procedure level, variables are available only within the
procedure.
You can also use the Dim statement with empty parentheses to
declare a dynamic array.
Note When you use the Dim statement in a procedure, you generally
put the Dim statement at the beginning of the procedure.
Dani Vainstein
16
Private Statement
Private varname[([subscripts])][, varname[([subscripts])]] . . .
Dani Vainstein
17
Public Statement
Public varname[([subscripts])][, varname[([subscripts])]] . . .
PublicMyNumber
MyNumber -----Public
PublicVariant
Variantvariable.
variable.
Public
PublicMyArray(9)
MyArray(9) -----Public
Publicarray
arrayvariable.
variable.
Public
---Multiple
MultiplePublic
Publicdeclarations
declarationsof
ofVariant
Variantvariables.
variables.
--PublicMyNumber,
MyNumber,MyVar,
MyVar,YourNumber
YourNumber
Public
Dani Vainstein
18
Rem Statement
Includes explanatory remarks in a program.
Rem Comment
Comment
As shown in the syntax section, you can use an apostrophe (')
instead of the Rem keyword.
If the Rem keyword follows other statements on a line, it must be
separated from the statements by a colon.
However, when you use an apostrophe, the colon is not required
after other statements.
.MyStr1=="Hello"
"Hello"::Rem
RemComment
Commentafter
afteraastatement
statementseparated
separatedby
byaacolon
colon
.MyStr1
.MyStr2=="Goodbye"
"Goodbye"' 'This
Thisisisalso
alsoaacomment;
comment;no
nocolon
colonisisneeded
needed
.MyStr2
.RemComment
Commenton
onaaline
linewith
withno
nocode;
code;no
nocolon
colonisisneeded
needed
.Rem
Dani Vainstein
19
For Example :
200
BB==200
Dani Vainstein
20
Lab 1.1
Dani Vainstein
21
Lab 1.1
Write a small program
Declare using Dim 2 variables (a,b)
Initialize the variables using a=10 and b=5
Apply the sum to variable c.
Report to QTP the sum of the variables.
Use remarks.
Tip : Conversion
Dani Vainstein
22
23