Advanced QTP
Advanced QTP
(In VBScript Orientation)
(In VBScript Orientation)
1)
1) InIntrtrododucuctitionon 2)
2) CComommmenenttss 3)
3) VB SVB Scrcripipt Vat Variriabableless 4)
4) VB SVB Scrcripipt Dat Data Tta Typypeses )
) VB SVB Scrcripipt Opet Operaratotorsrs !)
!) InpInput"ut"OutOutput Oput Operperatiationsons #)
#) CCononsstatanntsts $)
$) ConConditditionional Sal Stattatemeementsnts %)
%) &e&eneneraral 'l 'aampmpleless 1)*oop T+rou,+ Code 1)*oop T+rou,+ Code 11)-rocedures
11)-rocedures
12)Built.In /unctions 12)Built.In /unctions
13)VBScript synta rules and ,uidelines 13)VBScript synta rules and ,uidelines 14)'rrors
14)'rrors
1)/ile System Operations 1)/ile System Operations 1!)Test 0euirements 1!)Test 0euirements 1#) Solutions 1#) Solutions 1$)T- dd.Ins Inormation 1$)T- dd.Ins Inormation 1%) VBScript &lossary 1%) VBScript &lossary
For Complete Docment !isit:
For Complete Docment !isit:
www.gcreddy.com
o
o VBScript is a scriptin, lan,ua,e5VBScript is a scriptin, lan,ua,e5 o
o scriptin, lan,ua,e is a scriptin, lan,ua,e is a li,+t6ei,+t pro,rammin, lan,ua,e5li,+t6ei,+t pro,rammin, lan,ua,e5 o
o VBScript is a li,+t VBScript is a li,+t 7ersion o 8icrosot9s pro,rammin, lan,ua,e Visual Basic57ersion o 8icrosot9s pro,rammin, lan,ua,e Visual Basic5
:+en a VBScript is inserted into a ;T8*
:+en a VBScript is inserted into a ;T8* document< t+e Internet bro6ser 6ill readdocument< t+e Internet bro6ser 6ill read t+e ;T8* and interpret t+e VBScript5 T+e VBScript can be eecuted immediately< or t+e ;T8* and interpret t+e VBScript5 T+e VBScript can be eecuted immediately< or at a later e7ent5
at a later e7ent5
8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o 8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb ser7er scriptin, in 8icrosot Internet
ser7er scriptin, in 8icrosot Internet Inormation Ser7ice5Inormation Ser7ice5
#.# $indows Script %ost $S%'
#.# $indows Script %ost $S%'
It is a :indo6s administration tool5 :S; creates an en7ironment or +ostin, scripts5 It is a :indo6s administration tool5 :S; creates an en7ironment or +ostin, scripts5
T+at is< 6+en a script arri7es at your computer< :S; plays t+e part o
T+at is< 6+en a script arri7es at your computer< :S; plays t+e part o t+e +ost = itt+e +ost = it ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o
ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o ,uidelines,uidelines 6it+in 6+ic+ t+e script is eecuted5 mon, ot+er t+in,s< :indo6s Script ;ost 6it+in 6+ic+ t+e script is eecuted5 mon, ot+er t+in,s< :indo6s Script ;ost mana,es security and in7o>es t+e appropriate script
mana,es security and in7o>es t+e appropriate script en,ineen,ine
:indo6s Script ;ost is built into 8icrosot :indo6s %$< 2< and
:indo6s Script ;ost is built into 8icrosot :indo6s %$< 2< and 8illennium8illennium 'ditions and +i,+er 7ersions5
'ditions and +i,+er 7ersions5
:indo6s script is a tet
:indo6s script is a tet ile5 :e can create a script 6it+ any tet ile5 :e can create a script 6it+ any tet editor as lon, aseditor as lon, as 6e sa7e our script 6it+ a :S;.compatible script etension (5?s< 7bs< or 56s)5
6e sa7e our script 6it+ a :S;.compatible script etension (5?s< 7bs< or 56s)5 T+e most commonly a7ailable tet editor is already installed on our computer = T+e most commonly a7ailable tet editor is already installed on our computer = @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< or Visual InterDe75
or Visual InterDe75
#.( Creating a script wit) *otepad
#.( Creating a script wit) *otepad
15Start @otepad5 15Start @otepad5
25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script 25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script
35Sa7e t+is tet ile 6it+ a
35Sa7e t+is tet ile 6it+ a 57bs etension (instead o t+e deault 5tt etension)5 /or57bs etension (instead o t+e deault 5tt etension)5 /or eample< ;ello57bs
eample< ;ello57bs
45@a7i,ate to t+e ile you
45@a7i,ate to t+e ile you ?ust sa7ed< and double.clic> it5?ust sa7ed< and double.clic> it5
5:indo6s Script ;ost in7o>es t+e VB Script en,ine and runs your script5 In t+e 5:indo6s Script ;ost in7o>es t+e VB Script en,ine and runs your script5 In t+e eample< a messa,e bo is displayed 6it+ t+e messa,e ;ello VB
eample< a messa,e bo is displayed 6it+ t+e messa,e ;ello VB ScriptScript
#.+ %osting ,nvironments and Script ,ngines
Introdction
Introdction
o
o VBScript is a scriptin, lan,ua,e5VBScript is a scriptin, lan,ua,e5 o
o scriptin, lan,ua,e is a scriptin, lan,ua,e is a li,+t6ei,+t pro,rammin, lan,ua,e5li,+t6ei,+t pro,rammin, lan,ua,e5 o
o VBScript is a li,+t VBScript is a li,+t 7ersion o 8icrosot9s pro,rammin, lan,ua,e Visual Basic57ersion o 8icrosot9s pro,rammin, lan,ua,e Visual Basic5
:+en a VBScript is inserted into a ;T8*
:+en a VBScript is inserted into a ;T8* document< t+e Internet bro6ser 6ill readdocument< t+e Internet bro6ser 6ill read t+e ;T8* and interpret t+e VBScript5 T+e VBScript can be eecuted immediately< or t+e ;T8* and interpret t+e VBScript5 T+e VBScript can be eecuted immediately< or at a later e7ent5
at a later e7ent5
8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o 8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb ser7er scriptin, in 8icrosot Internet
ser7er scriptin, in 8icrosot Internet Inormation Ser7ice5Inormation Ser7ice5
#.# $indows Script %ost $S%'
#.# $indows Script %ost $S%'
It is a :indo6s administration tool5 :S; creates an en7ironment or +ostin, scripts5 It is a :indo6s administration tool5 :S; creates an en7ironment or +ostin, scripts5
T+at is< 6+en a script arri7es at your computer< :S; plays t+e part o
T+at is< 6+en a script arri7es at your computer< :S; plays t+e part o t+e +ost = itt+e +ost = it ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o
ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o ,uidelines,uidelines 6it+in 6+ic+ t+e script is eecuted5 mon, ot+er t+in,s< :indo6s Script ;ost 6it+in 6+ic+ t+e script is eecuted5 mon, ot+er t+in,s< :indo6s Script ;ost mana,es security and in7o>es t+e appropriate script
mana,es security and in7o>es t+e appropriate script en,ineen,ine
:indo6s Script ;ost is built into 8icrosot :indo6s %$< 2< and
:indo6s Script ;ost is built into 8icrosot :indo6s %$< 2< and 8illennium8illennium 'ditions and +i,+er 7ersions5
'ditions and +i,+er 7ersions5
:indo6s script is a tet
:indo6s script is a tet ile5 :e can create a script 6it+ any tet ile5 :e can create a script 6it+ any tet editor as lon, aseditor as lon, as 6e sa7e our script 6it+ a :S;.compatible script etension (5?s< 7bs< or 56s)5
6e sa7e our script 6it+ a :S;.compatible script etension (5?s< 7bs< or 56s)5 T+e most commonly a7ailable tet editor is already installed on our computer = T+e most commonly a7ailable tet editor is already installed on our computer = @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< or Visual InterDe75
or Visual InterDe75
#.( Creating a script wit) *otepad
#.( Creating a script wit) *otepad
15Start @otepad5 15Start @otepad5
25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script 25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script
35Sa7e t+is tet ile 6it+ a
35Sa7e t+is tet ile 6it+ a 57bs etension (instead o t+e deault 5tt etension)5 /or57bs etension (instead o t+e deault 5tt etension)5 /or eample< ;ello57bs
eample< ;ello57bs
45@a7i,ate to t+e ile you
45@a7i,ate to t+e ile you ?ust sa7ed< and double.clic> it5?ust sa7ed< and double.clic> it5
5:indo6s Script ;ost in7o>es t+e VB Script en,ine and runs your script5 In t+e 5:indo6s Script ;ost in7o>es t+e VB Script en,ine and runs your script5 In t+e eample< a messa,e bo is displayed 6it+ t+e messa,e ;ello VB
eample< a messa,e bo is displayed 6it+ t+e messa,e ;ello VB ScriptScript
#.+ %osting ,nvironments and Script ,ngines
interprets and runs t+e script code is loaded by
interprets and runs t+e script code is loaded by t+e :eb bro6ser< suc+ as Internett+e :eb bro6ser< suc+ as Internet 'plorer5
'plorer5
In t+e case o a
In t+e case o a script embedded in an S- pa,e< t+e en,ine script embedded in an S- pa,e< t+e en,ine t+at interprets and runst+at interprets and runs t+e script code is built into
t+e script code is built into Internet Inormation Ser7ices (IIS)5Internet Inormation Ser7ices (IIS)5
:indo6s Script ;ost eecutes scripts t+at eist outside an ;T8* or S- pa,e and :indo6s Script ;ost eecutes scripts t+at eist outside an ;T8* or S- pa,e and t+at stand on t+eir o6n as tet
t+at stand on t+eir o6n as tet iles5iles5
#.- Availale Script ,ngines
#.- Availale Script ,ngines
&enerally< 6e 6rite scripts in eit+er 8icrosot Script or VBScript< t+e t6o script &enerally< 6e 6rite scripts in eit+er 8icrosot Script or VBScript< t+e t6o script en,ines t+at s+ip 6it+ 8icrosot :indo6s %$< 2 and 8illennium 'ditions5 en,ines t+at s+ip 6it+ 8icrosot :indo6s %$< 2 and 8illennium 'ditions5
:e can use ot+er script en,ines< suc+ as -erl< 0'< and -yt+on< 6it+ :indo6s :e can use ot+er script en,ines< suc+ as -erl< 0'< and -yt+on< 6it+ :indo6s Script ;ost5
Script ;ost5
stand.alone script 6ritten in Script +as t+e 5?s etensionE a stand.alone script stand.alone script 6ritten in Script +as t+e 5?s etensionE a stand.alone script 6ritten in VBScript +as t+e 57bs etension5 T+ese etensions are re,istered 6it+ 6ritten in VBScript +as t+e 57bs etension5 T+ese etensions are re,istered 6it+ :indo6s5 :+en 6e run one o t+ese types
:indo6s5 :+en 6e run one o t+ese types o iles< :indo6s starts :indo6s Scripto iles< :indo6s starts :indo6s Script ;ost< 6+ic+ in7o>es t+e associated script en,ine to interpret and run t+e ile5
Comments
Comments
T+e comment ar,ument is t+e tet oT+e comment ar,ument is t+e tet o any comment 6e 6ant to include5any comment 6e 6ant to include5
(./ Prpose of comments:
(./ Prpose of comments:
o
o :e can use :e can use comments or ma>in, t+e script comments or ma>in, t+e script understandabunderstandable5le5 o
o :e can use comments or ma>in, one or more statements disable rom:e can use comments or ma>in, one or more statements disable rom
eecution5 eecution5
(.# Synta0
(.# Synta0
0em
0em comment comment (ter t+e 0em >ey6ord< a space is reuired beore (ter t+e 0em >ey6ord< a space is reuired beore comment5)comment5)
Or Or
postrop+e (9) symbol beore t+e comment postrop+e (9) symbol beore t+e comment
(.( Comment12ncomment a loc3 of
(.( Comment12ncomment a loc3 of statementsstatements
Select bloc> o statement and use s+ort cut >ey
Select bloc> o statement and use s+ort cut >ey Ctrl A 8 (or comment)Ctrl A 8 (or comment) Select comment bloc> and use s+ort cut >ey Ctrl A
Select comment bloc> and use s+ort cut >ey Ctrl A S+it A 8 (or uncomment)S+it A 8 (or uncomment)
(.+ ,0ample
!4 Script !ariales
7ariable is a con7enient place+older t+at reers to a computer memory location 6+ere 6e can store pro,ram inormation t+at may c+an,e durin, t+e time our script is runnin,5
+.# Declaring !ariales
:e declare 7ariables eplicitly in our script usin, t+e Dim statement< t+e -ublic statement< and t+e -ri7ate statement5
/or eampleF
Dim city Dim
:e declare multiple 7ariables by separatin, eac+ 7ariable name 6it+ a comma5 /or
'ampleF
Dim < Top< Bottom< *et< 0i,+t
:e can also declare a 7ariable implicitly by simply usin, its name in our script5 T+at is not ,enerally a ,ood practice because 6e could misspell t+e 7ariable name in one or more places< causin, unepected results 6+en our script is run5 /or t+at reason< t+e Option 'plicit statement is a7ailable to reuire eplicit declaration o all
7ariables5
T+e Option 'plicit statement s+ould be t+e irst statement in our script5
+.( 5ption ,0plicit
/orces eplicit declaration o all 7ariables in a script5
Option 'plicit 9 /orce eplicit 7ariable declaration5 Dim 8yVar 9 Declare 7ariable5
8yInt G 1 9 Hndeclared 7ariable ,enerates error5
8yVar G 1 9 Declared 7ariable does not ,enerate error5
+.+ *aming 6estrictions for !ariales
Variable names ollo6 t+e standard rules or namin, anyt+in, in VBScript5 7ariable nameF
o 8ust be,in 6it+ an alp+abetic c+aracter5 o Cannot contain an embedded period5 o 8ust not eceed 2 c+aracters5
o 8ust be uniue in t+e scope in 6+ic+ it is declared5
+.- Scope of !ariales
7ariable9s scope is determined by 6+ere 6e declare it5
:+en 6e declare a 7ariable 6it+in a procedure< only code 6it+in t+at procedure can access or c+an,e t+e 7alue o t+at 7ariable5
I 6e declare a 7ariable outside a procedure< 6e ma>e it reco,niable to all t+e procedures in our script5 T+is is a script.le7el 7ariable< and it +as script.le7el scope5
+.7 8ife Time of !ariales
T+e lietime o a 7ariable depends on +o6 lon, it eists5
T+e lietime o a script.le7el 7ariable etends rom t+e time it is declared until t+e time t+e script is inis+ed runnin,5
t procedure le7el< a 7ariable eists only as lon, as you are in t+e procedure5
+.9 Assigning !ales to !ariales
Values are assi,ned to 7ariables creatin, an epression as ollo6sF
T+e 7ariable is on t+e let side o t+e epression and t+e 7alue you 6ant to assi,n to t+e 7ariable is on t+e ri,+t5
/or eampleF G 2
City G J;yderabadK
G1F LG2
+. Scalar !ariales and Array !ariales
7ariable containin, a sin,le 7alue is a scalar 7ariable5
7ariable containin, a series o 7alues< is called an array 7ariable5
rray 7ariables and scalar 7ariables are declared in t+e same 6ay< ecept t+at t+e declaration o an array 7ariable uses parent+eses () ollo6in, t+e 7ariable name5
'ampleF Dim (3)
lt+ou,+ t+e number s+o6n in t+e parent+eses is 3< all arrays in VBScript are ero. based< so t+is array actually contains 4 elements5
() G 2! (1) G 324 (2) G 1 (3) G
Similarly< t+e data can be retrie7ed rom any element usin, an inde into t+e particular array element you 6ant5
/or eampleF
SomeVariable G (4)
rrays aren9t limited to a sin,le dimension5 :e can +a7e as many as ! dimensions< alt+ou,+ most people can9t compre+end more t+an t+ree or our dimensions5
In t+e ollo6in, eample< t+e 8yTable 7ariable is a t6o.dimensional array consistin, o ! ro6s and 11 columnsF
Dim 8yTable(< 1)
In a t6o.dimensional array< t+e irst number is al6ays t+e number o ro6sE t+e second number is t+e number o columns5
+.; Dynamic Arrays
:e can also declare an array 6+ose sie c+an,es durin, t+e time our script is runnin,5 T+is is called a dynamic array5
T+e array is initially declared 6it+in a procedure usin, eit+er t+e Dim statement or usin, t+e 0eDim statement5
;o6e7er< or a dynamic array< no sie or number o dimensions is placed inside t+e parent+eses5
/or eampleF Dim 8yrray()
0eDim not+errray()
To use a dynamic array< you must subseuently use 0eDim to determine t+e number o dimensions and t+e sie o eac+ dimension5
In t+e ollo6in, eample< 0eDim sets t+e initial sie o t+e dynamic array to 25 subseuent 0eDim statement resies t+e array to 3< but uses t+e -reser7e >ey6ord to preser7e t+e contents o t+e array as t+e resiin, ta>es place5
0eDim 8yrray(2)
T+ere is no limit to t+e number o times 6e can resie a dynamic array< alt+ou,+ i 6e ma>e an array smaller< 6e lose t+e data in t+e eliminated elements5
!4 Script Data Types
VBScript +as only one data type called a Variant5 Variant is a special >ind o data type t+at can contain dierent >inds o inormation< dependin, on +o6 it is used5
Because Variant is t+e only data type in VBScript< it is also t+e data type returned by all unctions in VBScript5
-.# !ariant Stypes
Beyond t+e simple numeric or strin, classiications< a Variant can ma>e urt+er distinctions about t+e speciic nature o numeric inormation5 /or eample< 6e can +a7e numeric inormation t+at represents a date or a time5 :+en used 6it+ ot+er date or time data< t+e result is al6ays epressed as a date or a time5 :e can also +a7e a ric+ 7ariety o numeric inormation ran,in, in sie rom Boolean 7alues to +u,e loatin,.point numbers5 T+ese dierent cate,ories o inormation t+at can be contained in a Variant are called subtypes5 8ost o t+e time< 6e can ?ust put t+e >ind o data 6e 6ant in a Variant< and t+e Variant be+a7es in a 6ay t+at is most
appropriate or t+e data it contains5
T+e ollo6in, table s+o6s subtypes o data t+at a Variant can contain5
Subtype Description
'mpty Variant is uninitialied5 Value is or numeric 7ariables or a ero.len,t+ strin, () or strin, 7ariables5
@ull Variant intentionally contains no 7alid data5 Boolean Contains eit+er True or /alse5
Byte Contains inte,er in t+e ran,e to 25
Inte,er Contains inte,er in t+e ran,e .32<#!$ to 32<#!#5
Currency .%22<33#<23<!$<4##5$$ to %22<33#<23<!$<4##5$#5 *on, Contains inte,er in t+e ran,e .2<14#<4$3<!4$ to 2<14#<4$3<!4#5
Sin,le Contains a sin,le.precision< loatin,.point number in t+e ran,e .3542$23'3$ to .15412%$'.4 or ne,ati7e 7aluesE 15412%$'.4 to 3542$23'3$ or positi7e 7alues5
DoubleContains a double.precision< loatin,.point number in t+e ran,e
.15#%#!%3134$!232'3$ to .45%4!!4$4124#'.324 or ne,ati7e 7aluesE 45%4!!4$4124#'.324 to 15#%#!%3134$!232'3$ or positi7e 7alues5
Date (Time) Contains a number t+at represents a date bet6een anuary 1< 1 to December 31< %%%%5
Strin, Contains a 7ariable.len,t+ strin, t+at can be up to approimately 2 billion c+aracters in len,t+5
Ob?ect Contains an ob?ect5
'rror Contains an error number5
:e can use con7ersion unctions to con7ert data rom one subtype to anot+er5 In addition< t+e VarType unction returns inormation about +o6 your data is stored 6it+in a Variant5
!4 Script 5perators
Operators are used or perormin, mat+ematical< comparison and lo,ical operations5 VBScript +as a ull ran,e o operators< includin, arit+metic operators< comparison operators< concatenation operators< and lo,ical operators5
-.# 5perator Precedence
:+en se7eral operations occur in an epression< eac+ part is e7aluated and resol7ed in a predetermined order called operator precedence5
:e can use parent+eses to o7erride t+e order o precedence and orce some parts o an epression to be e7aluated beore ot+ers5
Operations 6it+in parent+eses are al6ays perormed beore t+ose outside5 :it+in parent+eses< +o6e7er< standard operator precedence is maintained5
:+en epressions contain operators rom more t+an one cate,ory< arit+metic operators are e7aluated irst< comparison operators are e7aluated net< and lo,ical operators are e7aluated last5
Comparison operators all +a7e eual precedenceE t+at is< t+ey are e7aluated in t+e let.to.ri,+t order in 6+ic+ t+ey appear5
rit+metic and lo,ical operators are e7aluated in t+e ollo6in, order o precedence5
-.( Arit)metic 5perators:
5perator Description
1) 'ponentiation Operator (M) 0aises a number to t+e po6er o an eponent 2) 8ultiplication Operator (N) 8ultiplies t6o numbers5
3) Di7ision Operator (") Di7ides t6o numbers and returns a loatin,.point result5
4) Inte,er Di7ision Operator () Di7ides t6o numbers and returns an inte,er result5 ) 8od Operator Di7ides t6o numbers and returns only t+e remainder5 !) ddition Operator (A) Sums t6o numbers5
#) Subtraction Operator (.) /inds t+e dierence bet6een t6o numbers or indicates t+e ne,ati7e 7alue o a numeric epression5
$) Concatenation Operator (P) /orces strin, concatenation o t6o epressions5
-.+ Comparison 5perators
Hsed to compare epressions5
Operator Description
1) G ('ual to) Hsed to compare epressions5 2) QR (@ot eual to) Hsed to compare epressions5
3) Q *ess t+an 4) R &rater t+an
) QG *ess t+an or eual to !) RG &reater t+an or eual to
#) Is Ob?ect eui7alence
-.- Concatenation 5perators
5perator Description
1) ddition Operator <' Sums t6o numbers
If T)en
1) Bot+ epressions are numeric dd5
2) Bot+ epressions are strin,s Concatenate5 3) One epression is numeric and t+e
ot+er is a strin,
dd5
2) Concatenation Operator &'
/orces strin, concatenation o t6o epressions5
-.7 8ogical 5perators
5perator Description Synta0 1) @ot -erorms lo,ical ne,ation on an
epression
resultG @ot epression
2) nd -erorms a lo,ical con?unction on t6o epressions5
resultG epression1 nd epression2
3) Or -erorms a lo,ical dis?unction on t6o epressions5
resultG epression1 Or epression2
4) or -erorms a lo,ical eclusion on t6o epressions5
resultG epression1 or epression2
) '7 -erorms a lo,ical eui7alence on t6o epressions5
resultG epression1 '7 epression2
!) Imp -erorms a lo,ical implication on t6o epressions5
resultG epression1 Imp epression2
Inpt15tpt 5perations
9.# Inpt4o0 Fnction
Displays a prompt in a dialo, bo< 6aits or t+e user to input tet or clic> a button< and returns t+e contents o t+e tet bo5
'ampleF
Dim Input
Input G InputBo('nter your name) 8s,Bo (Lou enteredF P Input)
9.( "sg4o0 Fnction
Displays a messa,e in a dialo, bo< 6aits or t+e user to clic> a button< and returns a 7alue indicatin, 6+ic+ button t+e user clic>ed5
'ampleF
Dim 8yVar
8yVar G 8s,Bo (;ello :orld< !< 8s,Bo 'ample)
!4 Script Constants
constant is a meanin,ul name t+at ta>es t+e place o a number or strin, and ne7er c+an,es5
.# Creating Constants
:e create user.deined constants in VBScript usin, t+e Const statement5 Hsin, t+e Const statement< 6e can create strin, or numeric constants 6it+ meanin,ul names and assi,n t+em literal 7alues5
Const statement
Declares constants or use in place o literal 7alues5
'ampleF
Const 8yStrin, G T+is is my strin,5 Const 8y,e G 4%
Const CutoDate G !.1.%#
@ote t+at Strin, literal is enclosed in uotation mar>s ( )5
0epresent Date literals and time literals by enclosin, t+em in number si,ns ()5
:e declare multiple constants by separatin, eac+ constant name and 7alue 6it+ a comma5 /or eampleF
Conditional Statements
:e can control t+e lo6 o our script 6it+ conditional statements and loopin, statements5
Hsin, conditional statements< 6e can 6rite VBScript code t+at ma>es decisions and repeats actions5 T+e ollo6in, conditional statements are a7ailable in VBScriptF
#' If=T)en=,lse Statement
(' Select Case Statement
;.# "a3ing Decisions 2sing If...T)en...,lse
T+e I555T+en555'lse statement is used to e7aluate 6+et+er a condition is True or /alse and< dependin, on t+e result< to speciy one or more statements to run5
Hsually t+e condition is an epression t+at uses a comparison operator to compare one 7alue or 7ariable 6it+ anot+er5
I555T+en555'lse statements can be nested to as many le7els as you need5
;.#.# 6nning a Statement if a Condition is Tre single statement'
To run only one statement 6+en a condition is True< use t+e sin,le.line synta or t+e I555T+en555'lse statement5
Dim myDate
myDate G 2"13"%$
I myDate Q @o6 T+en myDate G @o6
;.#.( 6nning Statements if a Condition is Tre mltiple statements'
To run more t+an one line o code< 6e must use t+e multiple.line (or bloc>) synta5 T+is synta includes t+e 'nd I statement5
Dim G 2
I R1 T+en
ms,bo ;ello &5C50eddy ms,bo 7alue isF P ms,bo Bye Bye
'nd I
;.#.+ 6nning Certain Statements if a Condition is Tre and 6nning 5t)ers if a Condition is False
:e can use an I555T+en555'lse statement to deine t6o bloc>s o eecutable statementsF one bloc> to run i t+e condition is True< t+e ot+er bloc> to run i t+e condition is /alse5
'ampleF
Dim
G Inputbo ( 'nter a 7alue) I R1 T+en
8s,bo ;ello &5C50eddy 8s,bo is a Bi, @umber 8s,bo 7alue isF P 'lse
8s,bo &C0
8s,bo is a Small @umber 8s,bo 7alue isF P
'nd I
;.#.- Deciding 4etween Several Alternatives
7ariation on t+e I555T+en555'lse statement allo6s us to c+oose rom se7eral alternati7es5 ddin, 'lseI clauses epands t+e unctionality o t+e I555T+en555'lse statement so 6e can control pro,ram lo6 based on dierent possibilities5
'ampleF Dim
G Inputbo ( 'nter a 7alue)
I R and QG1 T+en
8s,bo ;ello &5C50eddy 8s,bo is a Small @umber 8s,bo 7alue is P
'lse I/ R1 and QG T+en 8s,bo ;ello &C0
8s,bo is a 8edium @umber
'lse I/ R and QG1 T+en
8s,bo ;ello C+andra 8o+an 0eddy 8s,bo is a *ar,e @umber
'lse
8s,bo ;ello Sir
8s,bo is a &rand @umber 'nd I
'nd I 'nd I
;.#.7 ,0ecting a certain loc3 of statements w)en two 1 more conditions are Tre *ested If...'
'ampleF
I stateG - T+en
I 0e,ionG Telan,ana T+en ms,bo ;ello &5C50eddy ms,bo Dist count is 1
'lse i 0e,ionG 0ayalasema T+en ms,bo ;ello &C0 ms,bo Dist count is 4
'lse I 0e,ionG Costal T+en
ms,bo ;ello C+andra mo+an 0eddy ms,bo Dist count is %
'nd I 'nd I 'nd I 'nd I
;.( "a3ing Decisions wit) Select Case
T+e Select Case structure pro7ides an alternati7e to I555T+en555'lseI or selecti7ely eecutin, one bloc> o statements rom amon, multiple bloc>s o statements5 Select Case statement pro7ides capability similar to t+e I555T+en555'lse statement< but it ma>es code more eicient and readable5
'ampleF
Option eplicit
Dim <y< Operation< 0esult G Inputbo ( 'nter 7alue) yG Inputbo ('nter y 7alue)
OperationG Inputbo ('nter an Operation)
Select Case Operation
Case add
0esultG cdbl ()Acdbl (y) 8s,bo ;ello &5C50eddy
8s,bo ddition o <y 7alues is P0esult
Case sub
0esultG .y
8s,bo ;ello &5C50eddy
8s,bo Substraction o <y 7alues is P0esult
Case mul
0esultG Ny
8s,bo ;ello &5C50eddy
Case di7
0esultG "y
8s,bo ;ello &5C50eddy
8s,bo Di7ision o <y 7alues is P0esult
Case mod
0esultG mod y
8s,bo ;ello &5C50eddy
8s,bo 8od o <y 7alues is P0esult
Case epo
0esultG My
8s,bo ;ello &5C50eddy
8s,bo'ponentation o <y 7alues is P0esult
Case 'lse
8s,bo ;ello &5C50eddy ms,bo :ron, Operation
'nd Select
;.+ 5t)er ,0amples
;.+.# $rite a program for finding ot w)et)er t)e given year is a leap year or not>
Dim year
yearGinputbo ('nter Lear)
I year mod 4G T+en
ms,bo T+is is a *eap year 'lse
ms,bo T+is is @OT 'nd I
;.+.( $rite a program for finding ot w)et)er t)e given nmer is? ,ven nmer or 5dd nmer>
Dim num
numGinputbo ('nter a number)
I num mod 2G T+en
ms,bo T+is is a '7en @umber 'lse
ms,bo T+is is a Odd @umber 'nd I
$5353 0ead t6o numbers and display t+e sumU
Dim num1<num2< sum
num1Ginputbo ('nter num1) num2Ginputbo ('nter num2)
ms,bo (Sum is Psum)
;.+.- 6ead P?T?6 vales and Calclate t)e Simple Interest>
Dim p<t< r< si
pGinputbo ('nter -rinciple) tGinputbo ('nter Time)
rGinputbo ('nter 0ate o Interest)
siG (pNtNr)"1 9 pG principle amount< tGtime in years< rG rate o interest ms,bo (Simple Interest is Psi)
;.+.7 6ead For digit nmer? calclate & display t)e sm of t)e nmer or display ,rror message if t)e nmer is not a for digit nmer>
Dim num< sum
numGinputbo ('nter a /our di,it number) I *en(num) G 4 T+en
sumG
sumGsumAnum mod 1
numGnum"1
numG let (num< 3) sumGsumAnum mod 1
numGnum"1
numG let (num< 2) sumGsumAnum mod 1
numGnum"1
numG let (num< 1) sumGsumAnum mod 1
ms,bo (Sum is Psum) else
ms,bo @umber< you entered is not a 4 di,it number 'nd I
;.+.9 6ead any For@digit nmer and display t)e nmer in reverse order>
Dim num<re7
numG inputbo('nter a number) I len(num)G4 T+en
re7Gre7N1 A num mod 1 numGnum"1
numG let(num<3)
re7Gre7N1 A num mod 1 numGnum"1
numG let(num<2)
re7Gre7N1 A num mod 1 numGnum"1
numG let(num<1)
re7Gre7N1 A num mod 1
ms,bo 0e7erse Order o t+e number is Pre7 'lse
ms,bo @umber< you entered is not a 4 di,it number 'nd I
;.+. 6ead - sects mar3sB calclate t)e Total mar3s and grade> (a) I a7era,e mar>s &reater t+an or eual to #< ,rade is Distinction
b) I a7era,e mar>s &reater t+an or eual to ! and less t+an # < t+en ,rade is /irst
c) I a7era,e mar>s &reater t+an or eual to and less t+an ! < t+en ,rade is Second
d) I a7era,e mar>s &reater t+an or eual to 4 and less t+an < t+en ,rade is T+ird
e) 8inimum mar>s 3 or any sub?ect< ot+er6ise 9no ,rade ail9)
Dim e<m<p<c< tot
eGinputbo ('nter en,lis+ 8ar>s) mGinputbo ('nter mat+s 8ar>s) pGinputbo ('nter p+ysics 8ar>s) cGinputbo ('nter c+emistry 8ar>s)
totG cdbl(e) A cdbl(m) A cdbl(p) A cdbl(c) ms,bo tot
I cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG3 T+en
ms,bo &rade is Distinction
else I cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG24 and totQ3 T+en
ms,bo &rade is /irst
else I cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG2 and totQ24 T+en
ms,bo &rade is Second
else I cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG1! and totQ2 T+en
ms,bo &rade is T+ird else
ms,bo @o &rade< /ail
'nd I 'nd I 'nd I 'nd I
nGInputbo ('nter a Vaule) /or numG 1 to n step 2
ms,bo num @et
;.+. Display ,ven nmers p to n>
Dim num<n
nGInputbo ('nter a Vaule) /or numG 2 to n step 2
ms,bo num @et
;.+.#/ display natral nmers p to n and write in a te0t file>
Dim num< n< so< myile
nG inputbo ('nter any Value) numG1
/or numG 1 to n step 1
Set soG createob?ect (scriptin,5ilesystemob?ect) set myileGso5opentetile ('F,cr5tt< $< true) myile56riteline num
myile5close @et
;.## Display *atral nmers in reverse order p to n>
Dim num<n
nGInputbo ('nter a Vaule) /or numGn to 1 step .1
ms,bo num @et
;.#( Display *atral nmers sm p to n> 2sing For...*e0t 8oop'
Dim num< n< sum
nG inputbo ('nter a Value) sumG
/or numG 1 to n step 1 sumG sumAnum @et
ms,bo sum
;.#+ Display *atral nmers sm p to n> sing $)ile...$end 8oop'
Dim num< n< sum
nG inputbo ('nter a Value) :+ile num QGcdbl (n)
numGnumA1 :end
ms,bo sum
;.#- Display *atral nmers sm p to n> 2sing Do...2ntil...8oop'
Dim num< n< sum
nG inputbo ('nter a Value) sumG
numG1 Do
sumG sumAnum numGnumA1
*oop Hntil num Gcdbl (nA1) ms,bo sum
;.#7 $rite a Fnction for *atral *mers sm p to n>
/unction @@umCou (n) Dim num< sum
sumG
/or numG 1 to n step 1 sumG sumAnum @et
ms,bo sum 'nd /unction
;.#9 !erify weat)er t)e entered #/ digit vale is a nmeric vale or not>
Dim a<<y<<num
numGInputbo ('nter a -+one @umber)
d1G let (num<1) d1G0i,+t (num<1)
d2Gmid (num< 2< len (1)) d3Gmid (num< 3< len (1)) d4Gmid (num< 4< len (1)) dGmid (num< < len (1)) d!Gmid (num< !< len (1)) d#Gmid (num< #< len (1)) d$Gmid (num< $< len (1)) d%Gmid (num< %< len (1))
I isnumeric (d1) G True and isnumeric (d2) G True and isnumeric (d3) G True and isnumeric (d4) G Trueand isnumeric (d) G Trueand isnumeric (d!) G
Trueand isnumeric (d#) G Trueand isnumeric (d$) G Trueand isnumeric (d%) G Trueand isnumeric (d1) G True T+en
ms,bo It is a @umeric Value else
;.# !erify weat)er t)e entered vale is a #/ digit vale or not and *meric vale or not> 2sing mltiple if conditions'
Dim a<<y<<num
numGInputbo ('nter a -+one @umber)
d1G let (num<1) d1G0i,+t (num<1)
d2Gmid (num< 2< len (1)) d3Gmid (num< 3< len (1)) d4Gmid (num< 4< len (1)) dGmid (num< < len (1)) d!Gmid (num< !< len (1)) d#Gmid (num< #< len (1)) d$Gmid (num< $< len (1)) d%Gmid (num< %< len (1))
I len (num) G1 T+en
I isnumeric (d1) G True and isnumeric (d2) G True and isnumeric (d3) G True and isnumeric (d4) G Trueand isnumeric (d) G Trueand isnumeric (d!) G
Trueand isnumeric (d#) G Trueand isnumeric (d$) G Trueand isnumeric (d%) G Trueand isnumeric (d1) G True T+en
ms,bo It is a @umeric Value 'nd I
'nd I
I len (num) QR 1 T+en
8s,bo It is @OT 7alid @umber 'nd I
8ooping T)rog) Code
o *oopin, allo6s us to run a ,roup o statements repeatedly5 o Some loops repeat statements until a condition is /alseE o Ot+ers repeat statements until a condition is True5
o T+ere are also loops t+at repeat statements a speciic number o times5
T)e following looping statements are availale in !4Script:
o Do...8oop: *oops 6+ile or until a condition is True5 o $)ile...$end: *oops 6+ile a condition is True5
o For...*e0t: Hses a counter to run statements a speciied number o times5 o For ,ac)...*e0t: 0epeats a ,roup o statements or eac+ item in a collection
or eac+ element o an array5
.# 2sing Do 8oops
:e can use Do555*oop statements to run a bloc> o statements an indeinite number o times5
T+e statements are repeated eit+er 6+ile a condition is True or until a condition becomes True5
.#.# 6epeating Statements $)ile a Condition is Tre
0epeats a bloc> o statements 6+ile a condition is True or until a condition becomes True a) Do :+ile condition Statements ... ... *oop
Or< 6e can use t+is belo6 syntaF
,0ample:
Dim
Do :+ile Q GA1
8s,bo ;ello &5C50eddy 8s,bo ;ello T-
*oop
b) Do
,0ample:
Dim G1 Do
8s,bo ;ello &5C50eddy 8s,bo ;ello T-
GA1
*oop :+ile Q
.#.( 6epeating a Statement 2ntil a Condition 4ecomes Tre
c) Do Hntil condition Statements
... ... *oop
Or< 6e can use t+is belo6 syntaF 'ampleF Dim Do Hntil G GA1 8s,bo &5C50eddy 8s,bo ;ello T- *oop
Or< 6e can use t+is belo6 syntaF d) Do
Statements ... ...
*oop Hntil condition
Or< 6e can use t+is belo6 syntaF
,0ample:
Dim G1 Do
8s,bo J;ello &5C50eddyK 8s,bo ;ello T-
GA1
*oop Hntil G
.( $)ile...$end Statement
SyntaF :+ile condition Statements ... ... :end ,0ample: Dim G :+ile Q GA1
ms,bo ;ello &5C50eddy ms,bo ;ello T-
:end
.+ For...*e0t Statement
0epeats a ,roup o statements a speciied number o times5 SyntaF
/or counter G start to end Step stepW statements
@et
,0ample: Dim
/or G 1 to step 1
8s,bo ;ello &5C50eddy @et
.- For ,ac)...*e0t Statement
0epeats a ,roup o statements or eac+ element in an array or collection5
SyntaF
/or 'ac+ item In array Statements @et 'ampleF (1 Dim a<b< (3) aG2 bG3 ()G ddition is P aAb (1)GSubstraction is P a.b (2)G 8ultiplication is P aNb (3)G Di7ision is P a"b
/or 'ac+ element In ms,bo element @et
/or 'ac+ element In 8yrray ms,bo element
@et
Control Flow ,0amples
2sing Conditional and 8oop Statements' ##.# read a nmer and verify t)at nmer 6ange weat)er in etween # to #// or #/# to #///>Option eplicit Dim a<
aGInputbo ('nter a Vaule) aGcdbl(a)
I aQG 1 T+en /or G 1 to 1 I aG T+en
ms,bo a is in bet6een 1 to 1 ran,e 'nd I
@et else
/or G 11 to 1 I aG T+en
ms,bo a is in bet6een 11 to 1 ran,e 'nd I
@et 'nd I
##.# read Data and find t)at data sie? If sie E- t)en display invalid data message? if data sie G - t)en verify Ha is t)ere or not in t)at data>
Dim
GInputbo ('nter 4 di,it 7alue) 1G0i,+t(<1)
2G*et (<1)
3Gmid (<2<*en(1)) 4Gmid (<3<*en(1)) yGlen()
I yG4 T+en
I 1Ga or 2Ga or 3Ga or 4Ga T+en ms,bo a is t+ere
else
ms,bo a is @ot t+ere 'nd I
else
ms,bo In7alid Data 'nd I
!4 Script Procedres
In VBScript< t+ere are t6o >inds o procedures a7ailableE t+e S procedure and t+e Fnction procedure5
##.# S Procedres
Sub procedure is a series o VBScript statements (enclosed by Sub and 'nd Sub statements) t+at perorm actions but don9t return a 7alue5
Sub procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed by a callin, procedure)5
I a Sub procedure +as no ar,uments< its Sub statement must include an empty set o parent+eses ()5
SyntaF
Sub -rocedure name () Statements
... ... 'nd Sub Or
Sub -rocedure name (ar,ument1< ar,ument2) Statements ... ... 'nd Sub ,0ample: # Sub Con7ertTemp()
temp G InputBo(-lease enter t+e temperature in de,rees /5< 1) 8s,Bo T+e temperature is P Celsius(temp) P de,rees C5 'nd Sub
,0ample: (
##.( Fnction Procedres
/unction procedure is a series o VBScript statements enclosed by t+e /unction and 'nd /unction statements5
/unction procedure is similar to a Sub procedure< but can also return a 7alue5
/unction procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed to it by a callin, procedure)5
/unction returns a 7alue by assi,nin, a 7alue to its name in one or more statements o t+e procedure5 T+e return type o a /unction is al6ays a Variant5 SyntaF
/unction -rocedure name () Statements
... ... 'nd /unction Or
/unction -rocedure name (ar,ument1< ar,ument2) Statements ... ... 'nd /unction ,0ample: # /unction Celsius(De,rees) Celsius G (De,rees . 32) N " % 'nd /unction ,0ample: ( /unction cal(a<b<c) cal G (aAbAc) 'nd /unction
##.+ Jetting Data into and ot of Procedres
o 'ac+ piece o data is passed into our procedures usin, an ar,ument5 o r,uments ser7e as place+olders or t+e data 6e 6ant to pass into our
procedure5 :e can name our ar,uments any 7alid 7ariable name5
o :+en 6e create a procedure usin, eit+er t+e Sub statement or t+e /unction
statement< parent+eses must be included ater t+e name o t+e procedure5
o ny ar,uments are placed inside t+ese parent+eses< separated by commas5
##.- 2sing S and Fnction Procedres in Code
/unction in our code must al6ays be used on t+e ri,+t side o a 7ariable assi,nment or in an epression5
/or eampleF
Temp G Celsius(De,rees) .Or.
To call a Sub procedure rom anot+er procedure< type t+e name o t+e procedure alon, 6it+ 7alues or any reuired ar,uments< eac+ separated by a comma5
T+e Call statement is not reuired< but i you do use it< you must enclose any ar,uments in parent+eses5
T+e ollo6in, eample s+o6s t6o calls to t+e 8y-roc procedure5 One uses t+e Call statement in t+e codeE t+e ot+er doesn9t5 Bot+ do eactly t+e same t+in,5
Call 8y-roc(irstar,< secondar,)
8y-roc irstar,< secondar,
@otice t+at t+e parent+eses are omitted in t+e call 6+en t+e Call statement isn9t used5
!4 Script 4ilt in Fnctions
Types of Fnctions o Con7ersions (2) o Dates"Times (1%) o /ormattin, Strin,s (4) o Input"Output (3) o 8at+ (%) o 8iscellaneous (3) o 0oundin, () o Strin,s (3) o Variants ($) Important Fnctions #' As Fnction0eturns t+e absolute 7alue o a number5 Dim num
numGabs(.533) ms,bo num
(' Array Fnction
0eturns a 7ariant containin, an rray Dim Grray(+yderabad<c+ennai<mumbai) ms,bo () 0eDim () (4)Gnellore ms,bo (4) +' Asc Fnction
0eturns t+e @SI c+aracter code correspondin, to t+e irst letter in a strin,5 Dim num
numGsc() ms,bo num
N It returns t+e 7alue ! N
-' C)r Fnction
0eturns t+e c+aracter associated 6it+ t+e speciied @SI c+aracter code5 Dim c+ar
C+arGC+r(!) ms,bo c+ar
N It returns N
7' CInt Fnction
0eturns an epression t+at +as been con7erted to a !ariant o subtype Integer5 Dim num
numG12354 myIntGCInt(num) ms,bo 8yInt
9' Date Fnction
0eturns t+e Current System Date5
Dim mydate mydateGDate ms,bo mydate ' Day Fnction '1) Dim myday mydayGDay(1#<December<2%) ms,bo myday '2) Dim myday mydateGdate mydayGDay(8ydate) ms,bo myday ;' DateDiff Fnction
0eturns t+e number o inter7als bet6een t6o dates5 Dim myday
mydateGK/(@#@(//K
0GDatediffLdL?mydate?*ow' msgo0 0
' %or Fnction
0eturns a 6+ole number bet6een and 23< inclusi7e< representin, t+e +our o t+e day5
Dim mytime< 8y+our mytimeG@o6
my+ourG+our (mytime) ms,bo my+our
#/' Moin Fnction
0eturns a strin, created by ?oinin, a number o substrin,s contained in an array5 Dim mystrin,< myarray(3)
myarray()GC+andra myarray(1)G8o+an myarray(2)G0eddy
##' ,val Fnction
'7aluates an epression and returns t+e result5
#(' Time Fnction
0eturns a !ariant o subtype Date indicatin, t+e current system time5 Dim mytime
mytimeGTime ms,bo mytime
#+' !arType Fnction
0eturns a 7alue indicatin, t+e subtype o a 7ariable5
Dim 8yC+ec>
8yC+ec> G VarType(3) 9 0eturns 25 8s,bo 8yc+ec>
8yC+ec> G VarType(1"1%"!2) 9 0eturns #5 8s,bo 8yc+ec>
8yC+ec> G VarType(VBScript) 9 0eturns $5 8s,bo 8yc+ec>
#-' 8eft Fnction
Dim 8yStrin,< *etStrin, 8yStrin, G VBSCript
*etStrin, G *et(8yStrin,< 3) 9 *etStrin, contains VBS5
#-' 6ig)t Fnction
Dim nyStrin,< 8yStr
nyStrin, G ;ello :orld 9 Deine strin,5 8yStr G 0i,+t(nyStrin,< 1) 9 0eturns d5 8yStr G 0i,+t(nyStrin,< !) 9 0eturns :orld5
8yStr G 0i,+t(nyStrin,< 2) 9 0eturns ;ello :orld5
#7' 8en Fnction
0eturns t+e number o c+aracters in a strin, or t+e number o bytes reuired to store a 7ariable5
' 1)F Dim 8ystrin,
mystrin,G*en(&5C50eddy) ms,bo mystrin,
' 2)F Dim 8ystrin,
8ystrin,GInputbo('nter a Value) 8ystrin,G*en(8ystrin,)
8s,bo 8ystrin,
#9' "id Fnction
0eturns a speciied number o c+aracters rom a strin,5 Dim 8yVar
8yVar G 8id(VB Script is un< 4< !) 8s,bo 8yVar
N It 0eturns XScriptY N
#' Timer Fnction
0eturns t+e number o seconds t+at +a7e elapsed since 12F 8 (midni,+t)5
/unction myTime(@)
Dim StartTime< 'ndTime StartTime G Timer
/or I G 1 To @ @et
'ndTime G Timer
myTimeG 'ndTime . StartTime ms,bo myTime
'nd /unction
Call myTime(2)
#' is*meric Fnction
Dim 8yVar< 8yC+ec> 8yVar G 3 8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec> 8yVar G 4%5% 8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec> 8yVar G 4 ;elp 8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>
N It 0eturns True"/alse li>e 0esult N
#;' Inpto0 Fnction
Displays a prompt in a dialo, bo< 6aits or t+e user to input tet or clic> a button< and returns t+e contents o t+e tet bo5
Dim Input
Input G InputBo('nter your name) 8s,Bo (Lou enteredF P Input)
Dim 8yVar
8yVar G 8s,Bo (;ello :orld< !< 8s,Bo 'ample)
!4Script synta0 rles and gidelines
(#.# Case@sensitivity:
By deault< VBScript is not case sensiti7e and does not dierentiate bet6een upper case and lo6er.case spellin, o 6ords< or eample< in 7ariables< ob?ect and met+od names< or constants5
/or eample< t+e t6o statements belo6 are identical in VBScriptF
Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:eb*ist(toDay)5Select 31 bro6ser(mercury)5pa,e(ind a li,+tF)56eblist(today)5select 31
(#.( Te0t strings:
:+en 6e enter a 7alue as a tet strin,< 6e must add uotation mar>s beore and ater t+e strin,5 /or eample< in t+e abo7e se,ment o script< t+e names o t+e :eb site< :eb pa,e< and edit bo are all tet strin,s surrounded by uotation mar>s5
@ote t+at t+e 7alue 31 is also surrounded by uotation mar>s< because it is a tet strin, t+at represents a number and not a numeric 7alue5
In t+e ollo6in, eample< only t+e property name (irst ar,ument) is a tet strin, and is in uotation mar>s5 T+e second ar,ument (t+e 7alue o t+e property) is a 7ariable and t+ereore does not +a7e uotation mar>s5 T+e t+ird ar,ument (speciyin, t+e timeout) is a numeric 7alue< 6+ic+ also does not need uotation mar>s5
Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:ait-roperty(items count< TotalZItems< 2)
(#.+ !ariales:
:e can speciy 7ariables to store strin,s< inte,ers< arrays and ob?ects5 Hsin, 7ariables +elps to ma>e our script more readable and leible
(#.- Parent)eses:
To ac+ie7e t+e desired result and to a7oid errors< it is important t+at 6e use parent+eses () correctly in our statements5
(#.7 Indentation:
:e can indent or outdent our script to relect t+e lo,ical structure and nestin, o t+e statements5
:e can add comments to our statements usin, an apostrop+e (9)< eit+er at t+e be,innin, o a separate line< or at t+e end o a statement5 It is recommended t+at 6e add comments 6+ere7er possible< to ma>e our scripts easier to understand and maintain5
(#. Spaces:
:e can add etra blan> spaces to our script to impro7e clarity5 T+ese spaces are i,nored by VBScript5
,rrors
:e +a7e t6o types 'rrors in VB ScriptE t+ey are VBScript 0un.time 'rrors and VBScript Synta 'rrors
#+.# !4Script 6n@time ,rrors
VBScript run.time errors are errors t+at result 6+en our VBScript script attempts to perorm an action t+at t+e system cannot eecute5 VBScript run.time errors occur 6+ile our script is bein, eecutedE 6+en 7ariable epressions are bein, e7aluated< and memory is bein, dynamic allocated5
#+.( !4Script Synta0 ,rrors
VBScript synta errors are errors t+at result 6+en t+e structure o one o our VBScript statements 7iolates one or more o t+e ,rammatical rules o t+e VBScript scriptin, lan,ua,e5 VBScript synta errors occur durin, t+e pro,ram compilation sta,e< beore t+e pro,ram +as be,un to be eecuted5
File System 5perations
I' $or3ing wit) Drives and Folders
a' Creating a Folder
Option 'plicit
Dim ob?/SO< ob?/older< strDirectory strDirectory G DFlo,s
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set ob?/older G ob?/SO5Create/older(strDirectory)
' Deleting a Folder
Set o/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) o/SO5Delete/older('F/SO)
c' Copying Folders
Set o/SOGcreateob?ect(Scriptin,5/ilesystemob?ect) o/SO5Copy/older 'F,cr!< CF?7r< True
d' C)ec3ing weat)er t)e folder availale or not? if not creating t)e folder Option 'plicit
Dim ob?/SO< ob?/older< strDirectory strDirectory G DFlo,s
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) I ob?/SO5/older'ists(strDirectory) T+en
Set ob?/older G ob?/SO5&et/older(strDirectory) ms,bo strDirectory P already created
else
Set ob?/older G ob?/SO5Create/older(strDirectory) end i
e' 6etrning a collection of Dis3 Drives
Set o/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set colDri7es G o/SO5Dri7es
/or 'ac+ oDri7e in colDri7es
8s,Bo Dri7e letterF P oDri7e5Dri7e*etter @et
f' Jetting availale space on a Dis3 Drive
Set o/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set oDri7e G o/SO5&etDri7e(CF)
a' Creating a Flat File
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set ob?/ile G ob?/SO5CreateTet/ile('FScript*o,5tt)
' C)ec3ing weat)er t)e File is availale or not? if not creating t)e File strDirectoryG'F
str/ileGScriptin,5tt
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) I ob?/SO5/ile'ists(strDirectory P str/ile) T+en
Set ob?/older G ob?/SO5&et/older(strDirectory) 'lse
Set ob?/ile G ob?/SO5CreateTet/ile('FScript*o,5tt) 'nd i
c' 6eading Data c)aracter y c)aracter from a Flat File Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set ob?/ile G ob?/SO5OpenTet/ile('F,cr5tt< 1)
Do Hntil ob?/ile5t'ndOStream strC+aracters G ob?/ile50ead(1) ms,bo strC+aracters
*oop
d' 6eading Data line y line from a Flat File
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set ob?/ile G ob?/SO5OpenTet/ile('F,cr5tt< 1)
Do Hntil ob?/ile5t'ndOStream strC+aracters G ob?/ile50eadline ms,bo strC+aracters
*oop
e' 6eading data from a flat file and sing in data driven testing
Dim so<myile
Set soGcreateob?ect(scriptin,5ilesystemob?ect) Set myileG so5opentetile (/F,cr5tt<1)
myile5s>ipline
:+ile myile5atendoline QR True Gmyile5readline
sGsplit (< <)
SystemHtil50un CF-ro,ram /iles8ercury Interacti7euic>Test
-roessionalsamplesli,+tappli,+t4a5ee<<CF-ro,ram /iles8ercury Interacti7euic>Test -roessionalsamplesli,+tapp<open
Dialo,(*o,in)5cti7ate Dialo,(*o,in)5:in'dit(,ent @ameF)5Set s() Dialo,(*o,in)5:in'dit(-ass6ordF)5SetSecure s(1) Dialo,(*o,in)5:inButton(O[)5Clic> :indo6(/li,+t 0eser7ation)5Close :end
f' $riting data to a te0t file
Dim Stu< my/SO< :riteStu< dateStamp dateStamp G Date()
Stu G I am -reparin, t+is scriptF PdateStamp
Set my/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set :riteStu G my/SO5OpenTet/ile(eF,cr5tt< $< True) :riteStu5:rite*ine(Stu)
:riteStu5Close
S'T :riteStu G @OT;I@& S'T my/SO G @OT;I@&
g' Delete a te0t file
Set ob?/SOGcreateob?ect(Scriptin,5ilesystemob?ect) Set tt/ilepat+ G ob?/SO5&et/ile('F,cr5tt)
tt/ilepat+5Delete()
)' C)ec3ing weat)er t)e File is availale or not? if availale delete t)e File
strDirectoryG'F str/ileG,cr5tt
Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) I ob?/SO5/ile'ists(strDirectory P str/ile) T+en
Set ob?/ile G ob?/SO5&etile(strDirectory P str/ile) ob?/ile5delete ()
'nd i
i' Comparing two te0t files
Dim 1< 2
1GeF,cr15tt 2GeF,cr25tt
-ublic /unction Compare/iles (/ile-at+1< /ile-at+2) Dim /S< /ile1< /ile2
Set /S G CreateOb?ect(Scriptin,5/ileSystemOb?ect)
I /S5&et/ile(/ile-at+1)5Sie QR /S5&et/ile(/ile-at+2)5Sie T+en Compare/iles G True
'it /unction 'nd I
Set /ile1 G /S5&et/ile(/ile-at+1)5OpensTetStream(1< ) Set /ile2 G /S5&et/ile(/ile-at+2)5OpensTetStream(1< )
Str1 G /ile150ead Str2 G /ile250ead
Compare/iles G StrComp(Str1< Str2< )
I Compare/iles QR T+en Compare/iles G True 'it Do 'nd I *oop /ile15Close() /ile25Close() 'nd /unction Call Compareiles(1<2)
I Compare/iles(1< 2) G /alse T+en 8s,Bo /iles are identical5
'lse
8s,Bo /iles are dierent5 'nd I
' Conting t)e nmer of times a word appears in a file
s/ile@ameG'F,cr5tt sStrin,G,creddy
Const /O0Z0'DI@& G 1
Dim o/so< oTt/ile< s0eadTt< o0e,'< o8atc+es
Set o/so G CreateOb?ect(Scriptin,5/ileSystemOb?ect)
Set oTt/ile G o/so5OpenTet/ile(s/ile@ame< /O0Z0'DI@&) s0eadTt G oTt/ile50eadll
Set o0e,' G @e6 0e,'p o0e,'5-attern G sStrin,
o0e,'5I,noreCase G bI,noreCase o0e,'5&lobal G True
Set o8atc+es G o0e,'5'ecute(s0eadTt) 8atc+es/ound G o8atc+es5Count
Set oTt/ile G @ot+in, F Set o/so G @ot+in, F Set o0e,' G @ot+in, ms,bo 8atc+es/ound
III' $or3ing wit) $ord Docs
a' Create a word docment and enter some data & save
Dim ob?:D
Set ob?:D G CreateOb?ect(:ord5pplication) ob?:D5Documents5dd
ob?:D5Selection5TypeTet T+is is some tet5 P C+r(13) P T+is is some more tet
ob?:D5cti7eDocument5Sa7es eF,creddy5doc ob?:D5uit
I!' $or3ing wit) ,0cel S)eets
a' Create an e0cel s)eet and enter a vale into first cell
Dim ob?ecel
Set ob?'cel G createob?ect('cel5application) ob?ecel5Visible G True
ob?ecel5:or>boo>s5add
ob?ecel5Cells(1< 1)5Value G Testin,
ob?ecel5cti7e:or>boo>5Sa7es(F,creddy15ls) ob?ecel5uit
' Compare two e0cel files
Set ob?'cel G CreateOb?ect('cel5pplication) ob?'cel5Visible G True
Set ob?:or>boo>1G ob?'cel5:or>boo>s5Open('F,cr15ls) Set ob?:or>boo>2G ob?'cel5:or>boo>s5Open('F,cr25ls)
Set ob?:or>s+eet1G ob?:or>boo>15:or>s+eets(1)
Set ob?:or>s+eet2G ob?:or>boo>25:or>s+eets(1)
/or 'ac+ cell In ob?:or>s+eet15Hsed0an,e
I cell5Value QR ob?:or>s+eet250an,e(cell5ddress)5Value T+en ms,bo 7alue is dierent
'lse
ms,bo 7alue is same 'nd I @et ob?:or>boo>15close ob?:or>boo>25close ob?'cel5uit set ob?'celGnot+in,
Test 6eNirements
1) !erify 8ogin 4ondary (C+ec> all t+e boundary conditions o t+e *o,in 6indo65 C+ec>s to see i t+e correct messa,e appears in t+e error 6indo6 (/li,+t 0eser7ation 8essa,e)
2) !erify Cancel 5peration (in *o,in Dialo, bo< i user selects cancel button< beore enter any data ater enter data dialo, bo s+ould be disappeared5)
+' !erify Addition? Straction? "ltiplication and Division 5perations in Calclator Application.
4) !erify state of 2pdate 5rder 4tton? efore open an 5rder and after open an 5rder (in /li,+t 0eser7ation beore openin, an order Hpdate Order button s+ould be disabled ater openin, an order enabled5)
) Price Consistency? In Flig)t 6eservation (In /li,+t 0eser7ation< /irst class priceG3N'conomy class price and Business class priceG2N'conomy class
price)
!) !erify Total? In Flig)t 6eservation (In /li,+t 0eser7ation< Total G Tic>ets N -rice)
#) !erify Flig)t From & Flig)t To Como 4o0es (In /li,+t reser7ation< select an item rom /ly /romF combo bo and 7eriy 6eat+er t+at item a7ailable or not in /ly ToF combo bo< li>e t+is select all items one by one in /ly /rom and 7eriy 6eat+er selected items a7ailable or not in /ly To5)
$) !erify 5rder *o ,ntry in Flig)t 6eservation. (In Open Order dialo, bo< Order @o ob?ect accepts numeric 7alues only5)
' Jet Test Data from a Flat file and se in Data Driven Testing t)rog) Scripting'
#/' Jet Test Data From a Dataase and se in Data Driven Testing t)rog) Scripting'
##' Cont? )ow many lin3s availale in "ercry Tors %ome Page>
#(' Cont )ow many 4ttons and ,dit o0es availale in Flig)t 6eservation window>
#+' !erify searc) options in 5pen 5rder Dialog o0
ter selectin, open order< 3 searc+ options s+ould be enabled and not c+ec>ed< ter selectin, Order @o option< ot+er options s+ould be disabled<
ter selectin, Customer @ame< /li,+t date option enabled and Order @o disabled ter selectin, /li,+t date option< Customer @ame enabled and Order @o disabled'
#-' In 8ogin Dialog o0? !erify %elp message (T+e messa,e is XT+e pass6ord is 98'0CH0L9)
#7' Cont all opened 4rowsers on des3top and close all>
#9' Create an ,0cel file? enter some data and save t)e file t)rog) !4 scripting>