VB Script
VB Script
com
Advanced QTP
(In VBScript Orientation)
1) Introduction 2) Comments 3) VB Script Variables 4) VB Script Data Types ) VB Script Operators !) Input"Output Operations #) Constants $) Conditional Statements %) &eneral '(amples 1))*oop T+rou,+ Code 11)-rocedures 12)Built.In /unctions 13)VBScript synta( rules and ,uidelines 14)'rrors 1 )/ile System Operations 1!)Test 0e1uirements 1#) Solutions 1$)2T- 3dd.Ins In4ormation 1%) VBScript &lossary
www.gcreddy.com
www.gcreddy.net
Introd ction
o o o VBScript is a scriptin, lan,ua,e5 3 scriptin, lan,ua,e is a li,+t6ei,+t pro,rammin, lan,ua,e5 VBScript is a li,+t 7ersion o4 8icroso4t9s pro,rammin, lan,ua,e Visual Basic5
:+en a VBScript is inserted into a ;T8* document< t+e Internet bro6ser 6ill read t+e ;T8* and interpret t+e VBScript5 T+e VBScript can be e(ecuted immediately< or at a later e7ent5 8icroso4t Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o4 en7ironments< includin, :eb client scriptin, in 8icroso4t Internet '(plorer and :eb ser7er scriptin, in 8icroso4t Internet In4ormation Ser7ice5 #.# $indows Script %ost &$S%' It is a :indo6s administration tool5 :S; creates an en7ironment 4or +ostin, scripts5 T+at is< 6+en a script arri7es at your computer< :S; plays t+e part o4 t+e +ost = it ma>es ob?ects and ser7ices a7ailable 4or t+e script and pro7ides a set o4 ,uidelines 6it+in 6+ic+ t+e script is e(ecuted5 3mon, ot+er t+in,s< :indo6s Script ;ost mana,es security and in7o>es t+e appropriate script en,ine :indo6s Script ;ost is built into 8icroso4t :indo6s %$< 2)))< and 8illennium 'ditions and +i,+er 7ersions5 3 :indo6s script is a te(t 4ile5 :e can create a script 6it+ any te(t editor as lon, as 6e sa7e our script 6it+ a :S;.compatible script e(tension (5?s< 7bs< or 56s4)5 T+e most commonly a7ailable te(t editor is already installed on our computer = @otepad5 :e can also use your 4a7orite ;T8* editor< Vbs'dit< 8icroso4t Visual CAA< or Visual InterDe75 #.( Creating a script wit) *otepad 15Start @otepad5 25:rite your script5 /or e(ample purposes< type 8s,bo( B;ello VB ScriptB 35Sa7e t+is te(t 4ile 6it+ a 57bs e(tension (instead o4 t+e de4ault 5t(t e(tension)5 /or e(ample< ;ello57bs 45@a7i,ate to t+e 4ile you ?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 e(ample< a messa,e bo( is displayed 6it+ t+e messa,e B;ello VB ScriptB #.+ %osting ,nvironments and Script ,ngines
www.gcreddy.net
www.gcreddy.net
Comments
T+e comment ar,ument is t+e te(t o4 any comment 6e 6ant to include5 (./ P rpose of comments: o o :e can use comments 4or ma>in, t+e script understandable5 :e can use comments 4or ma>in, one or more statements disable 4rom e(ecution5
(.# Synta0 0em comment (34ter t+e 0em >ey6ord< a space is re1uired be4ore comment5) Or 3postrop+e (9) symbol be4ore t+e comment (.( Comment12ncomment a .loc3 of statements Select bloc> o4 statement and use s+ort cut >ey Ctrl A 8 (4or comment) Select comment bloc> and use s+ort cut >ey Ctrl A S+i4t A 8 (4or uncomment) (.+ ,0ample
www.gcreddy.net
!4 Script !aria.les
3 7ariable is a con7enient place+older t+at re4ers to a computer memory location 6+ere 6e can store pro,ram in4ormation t+at may c+an,e durin, t+e time our script is runnin,5 +.# Declaring !aria.les :e declare 7ariables e(plicitly in our script usin, t+e Dim statement< t+e -ublic statement< and t+e -ri7ate statement5 /or e(ampleF Dim city Dim ( :e declare multiple 7ariables by separatin, eac+ 7ariable name 6it+ a comma5 /or '(ampleF Dim (< Top< Bottom< *e4t< 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, une(pected results 6+en our script is run5 /or t+at reason< t+e Option '(plicit statement is a7ailable to re1uire e(plicit declaration o4 all 7ariables5 T+e Option '(plicit statement s+ould be t+e 4irst statement in our script5 +.( 5ption ,0plicit /orces e(plicit declaration o4 all 7ariables in a script5 Option '(plicit 9 /orce e(plicit 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 !aria.les Variable names 4ollo6 t+e standard rules 4or namin, anyt+in, in VBScript5 3 7ariable nameF o o o 8ust be,in 6it+ an alp+abetic c+aracter5 Cannot contain an embedded period5 8ust not e(ceed 2 c+aracters5
www.gcreddy.net
+.- Scope of !aria.les 3 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 o4 t+at 7ariable5 I4 6e declare a 7ariable outside a procedure< 6e ma>e it reco,niIable 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 !aria.les T+e li4etime o4 a 7ariable depends on +o6 lon, it e(ists5 T+e li4etime o4 a script.le7el 7ariable e(tends 4rom t+e time it is declared until t+e time t+e script is 4inis+ed runnin,5 3t procedure le7el< a 7ariable e(ists only as lon, as you are in t+e procedure5 +.9 Assigning !al es to !aria.les Values are assi,ned to 7ariables creatin, an e(pression as 4ollo6sF T+e 7ariable is on t+e le4t side o4 t+e e(pression and t+e 7alue you 6ant to assi,n to t+e 7ariable is on t+e ri,+t5 /or e(ampleF 3 G 2)) City G J;yderabadK DG1))F LG2)) +.: Scalar !aria.les and Array !aria.les 3 7ariable containin, a sin,le 7alue is a scalar 7ariable5 3 7ariable containin, a series o4 7alues< is called an array 7ariable5 3rray 7ariables and scalar 7ariables are declared in t+e same 6ay< e(cept t+at t+e declaration o4 an array 7ariable uses parent+eses () 4ollo6in, t+e 7ariable name5 '(ampleF Dim 3(3) 3lt+ou,+ t+e number s+o6n in t+e parent+eses is 3< all arrays in VBScript are Iero. based< so t+is array actually contains 4 elements5 :e assi,n data to eac+ o4 t+e elements o4 t+e array usin, an inde( into t+e array5 For "an al Testing doc ments visit:
www.gcreddy.net
Similarly< t+e data can be retrie7ed 4rom any element usin, an inde( into t+e particular array element you 6ant5 /or e(ampleF SomeVariable G 3(4) 3rrays 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 4our dimensions5 In t+e 4ollo6in, e(ample< t+e 8yTable 7ariable is a t6o.dimensional array consistin, o4 ! ro6s and 11 columnsF Dim 8yTable( < 1)) In a t6o.dimensional array< t+e 4irst number is al6ays t+e number o4 ro6sE t+e second number is t+e number o4 columns5 +.; Dynamic Arrays :e can also declare an array 6+ose siIe 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< 4or a dynamic array< no siIe or number o4 dimensions is placed inside t+e parent+eses5 /or e(ampleF Dim 8y3rray() 0eDim 3not+er3rray() To use a dynamic array< you must subse1uently use 0eDim to determine t+e number o4 dimensions and t+e siIe o4 eac+ dimension5 In t+e 4ollo6in, e(ample< 0eDim sets t+e initial siIe o4 t+e dynamic array to 2 5 3 subse1uent 0eDim statement resiIes t+e array to 3)< but uses t+e -reser7e >ey6ord to preser7e t+e contents o4 t+e array as t+e resiIin, ta>es place5 0eDim 8y3rray(2 ) 0eDim -reser7e 8y3rray(3))
www.gcreddy.net
www.gcreddy.net
!4 Script 5perators
Operators are used 4or per4ormin, mat+ematical< comparison and lo,ical operations5 VBScript +as a 4ull ran,e o4 operators< includin, arit+metic operators< comparison operators< concatenation operators< and lo,ical operators5 -.# 5perator Precedence :+en se7eral operations occur in an e(pression< eac+ part is e7aluated and resol7ed in a predetermined order called operator precedence5 :e can use parent+eses to o7erride t+e order o4 precedence and 4orce some parts o4 an e(pression to be e7aluated be4ore ot+ers5 Operations 6it+in parent+eses are al6ays per4ormed be4ore t+ose outside5 :it+in parent+eses< +o6e7er< standard operator precedence is maintained5 :+en e(pressions contain operators 4rom more t+an one cate,ory< arit+metic operators are e7aluated 4irst< comparison operators are e7aluated ne(t< and lo,ical operators are e7aluated last5 Comparison operators all +a7e e1ual precedenceE t+at is< t+ey are e7aluated in t+e le4t.to.ri,+t order in 6+ic+ t+ey appear5 3rit+metic and lo,ical operators are e7aluated in t+e 4ollo6in, order o4 precedence5 -.( Arit)metic 5perators: 5perator 1) '(ponentiation Operator (M) 2) 8ultiplication Operator (N) 3) Di7ision Operator (") 4) ) !) #) Inte,er Di7ision Operator (O) 8od Operator 3ddition Operator (A) Subtraction Operator (.) Description 0aises a number to t+e po6er o4 an e(ponent 8ultiplies t6o numbers5 Di7ides t6o numbers and returns a 4loatin,.point result5 Di7ides t6o numbers and returns an inte,er result5 Di7ides t6o numbers and returns only t+e remainder5 Sums t6o numbers5 /inds t+e di44erence bet6een t6o numbers or indicates t+e ne,ati7e 7alue o4 a numeric e(pression5 /orces strin, concatenation o4 t6o e(pressions5
$) Concatenation Operator (P) -.+ Comparison 5perators Hsed to compare e(pressions5 Operator 1) G ('1ual to) 2) QR (@ot e1ual to)
www.gcreddy.net
-.- Concatenation 5perators 5perator 1) 3ddition Operator &<' Description Sums t6o numbers If T)en 1) Bot+ e(pressions are numeric 3dd5 2) Bot+ e(pressions are strin,s Concatenate5 3) One e(pression is numeric and t+e 3dd5 ot+er is a strin, /orces strin, concatenation o4 t6o e(pressions5
-.7 8ogical 5perators 5perator 1) @ot 2) 3nd 3) Or 4) Dor ) '17 !) Imp Description -er4orms lo,ical ne,ation on an e(pression -er4orms a lo,ical con?unction on t6o e(pressions5 -er4orms a lo,ical dis?unction on t6o e(pressions5 -er4orms a lo,ical e(clusion on t6o e(pressions5 -er4orms a lo,ical e1ui7alence on t6o e(pressions5 -er4orms a lo,ical implication on t6o e(pressions5 Synta0 resultG @ot e(pression resultG e(pression1 e(pression2 resultG e(pression1 e(pression2 resultG e(pression1 e(pression2 resultG e(pression1 e(pression2 3nd Or Dor '17
www.gcreddy.net
10
www.gcreddy.net
11
!4 Script Constants
3 constant is a meanin,4ul name t+at ta>es t+e place o4 a number or strin, and ne7er c+an,es5 :.# Creating Constants :e create user.de4ined constants in VBScript usin, t+e Const statement5 Hsin, t+e Const statement< 6e can create strin, or numeric constants 6it+ meanin,4ul names and assi,n t+em literal 7alues5 Const statement Declares constants 4or use in place o4 literal 7alues5 '(ampleF Const 8yStrin, G BT+is is my strin,5B Const 8y3,e G 4% Const Cuto44Date G T!.1.%#T @ote t+at Strin, literal is enclosed in 1uotation mar>s (B B)5 0epresent Date literals and time literals by enclosin, t+em in number si,ns (T)5 :e declare multiple constants by separatin, eac+ constant name and 7alue 6it+ a comma5 /or e(ampleF Const priceG 1))< cityG J;yderabadK< (G 2#
www.gcreddy.net
12
Conditional Statements
:e can control t+e 4lo6 o4 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 4ollo6in, conditional statements are a7ailable in VBScriptF
#' If=T)en=,lse Statement (' Select Case Statement ;.# "a3ing Decisions 2sing If...T)en...,lse
T+e I4555T+en555'lse statement is used to e7aluate 6+et+er a condition is True or /alse and< dependin, on t+e result< to speci4y one or more statements to run5 Hsually t+e condition is an e(pression t+at uses a comparison operator to compare one 7alue or 7ariable 6it+ anot+er5 I4555T+en555'lse statements can be nested to as many le7els as you need5 ;.#.# 6 nning a Statement if a Condition is Tr e &single statement' To run only one statement 6+en a condition is True< use t+e sin,le.line synta( 4or t+e I4555T+en555'lse statement5 Dim myDate myDate G T2"13"%$T I4 myDate Q @o6 T+en myDate G @o6 ;.#.( 6 nning Statements if a Condition is Tr e &m ltiple statements' To run more t+an one line o4 code< 6e must use t+e multiple.line (or bloc>) synta(5 T+is synta( includes t+e 'nd I4 statement5 Dim ( (G 2) I4 (R1) T+en ms,bo( B;ello &5C50eddyB ms,bo( B( 7alue isF BP( ms,bo( BBye ByeB 'nd I4 ;.#.+ 6 nning Certain Statements if a Condition is Tr e and 6 nning 5t)ers if a Condition is False
www.gcreddy.net
13
www.gcreddy.net
14
Case BmulB
www.gcreddy.net
15
0esultG ("y 8s,bo( B;ello &5C50eddyB 8s,bo( BDi7ision o4 (<y 7alues is BP0esult 0esultG ( mod y 8s,bo( B;ello &5C50eddyB 8s,bo( B8od o4 (<y 7alues is BP0esult
Case BmodB
Case Be(poB
0esultG (My 8s,bo( B;ello &5C50eddyB 8s,bo(B'(ponentation o4 (<y 7alues is BP0esult 8s,bo( B;ello &5C50eddyB ms,bo( B:ron, OperationB
Case 'lse
'nd Select
www.gcreddy.net
16
sumG Cdbl (num1) A Cdbl (num2) 9i4 6e 6ant add t6o strin,s con7ersion re1uire ms,bo( (BSum is B Psum)
;.+.- 6ead P?T?6 val es and Calc late t)e Simple Interest> Dim p<t< r< si pGinputbo( (B'nter -rincipleB) tGinputbo( (B'nter TimeB) rGinputbo( (B'nter 0ate o4 InterestB) siG (pNtNr)"1)) 9 pG principle amount< tGtime in years< rG rate o4 interest ms,bo( (BSimple Interest is B Psi) ;.+.7 6ead Fo r digit n m.er? calc late & display t)e s m of t)e n m.er or display ,rror message if t)e n m.er is not a fo r digit n m.er> Dim num< sum numGinputbo( (B'nter a /our di,it numberB) I4 *en(num) G 4 T+en sumG) sumGsumAnum mod 1) numGnum"1) numG le4t (num< 3) sumGsumAnum mod 1) numGnum"1) numG le4t (num< 2) sumGsumAnum mod 1) numGnum"1) numG le4t (num< 1) sumGsumAnum mod 1) ms,bo( (BSum is B Psum) else ms,bo( B@umber< you entered is not a 4 di,it numberB 'nd I4 ;.+.9 6ead any Fo r@digit n m.er and display t)e n m.er in reverse order> Dim num<re7 numG inputbo((B'nter a numberB) I4 len(num)G4 T+en re7Gre7N1) A num mod 1) numGnum"1) numG le4t(num<3) re7Gre7N1) A num mod 1) numGnum"1) numG le4t(num<2) re7Gre7N1) A num mod 1) numGnum"1)
www.gcreddy.net
17
;.+.: 6ead - s .Aects mar3sB calc late t)e Total mar3s and grade> (a) I4 a7era,e mar>s &reater t+an or e1ual to # < ,rade is Distinction b) I4 a7era,e mar>s &reater t+an or e1ual to !) and less t+an # < t+en ,rade is /irst c) I4 a7era,e mar>s &reater t+an or e1ual to ) and less t+an !) < t+en ,rade is Second d) I4 a7era,e mar>s &reater t+an or e1ual to 4) and less t+an ) < t+en ,rade is T+ird e) 8inimum mar>s 3 4or any sub?ect< ot+er6ise 9no ,rade 4ail9) Dim e<m<p<c< tot eGinputbo( (B'nter en,lis+ 8ar>sB) mGinputbo( (B'nter mat+s 8ar>sB) pGinputbo( (B'nter p+ysics 8ar>sB) cGinputbo( (B'nter c+emistry 8ar>sB) totG cdbl(e) A cdbl(m) A cdbl(p) A cdbl(c) ms,bo( tot I4 cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG3)) T+en ms,bo( B&rade is DistinctionB else I4 cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG24) and totQ3)) T+en ms,bo( B&rade is /irstB else I4 cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG2)) and totQ24) T+en ms,bo( B&rade is SecondB else I4 cdbl(e) RG3 and cdbl(m) RG3 and cdbl(p) RG3 and cdbl(c) RG3 and tot RG1!) and totQ2)) T+en ms,bo( B&rade is T+irdB else ms,bo( B@o &rade< /ailB 'nd 'nd 'nd 'nd I4 I4 I4 I4
www.gcreddy.net
18
Dim num< n< 4so< my4ile nG inputbo( (B'nter any ValueB) numG1 /or numG 1 to n step 1 Set 4soG createob?ect (Bscriptin,54ilesystemob?ectB) set my4ileG4so5opente(t4ile (B'FO,cr5t(tB< $< true) my4ile56riteline num my4ile5close @e(t ;.## Display *at ral n m.ers in reverse order Dim num<n nGInputbo( (B'nter a VauleB) /or numGn to 1 step .1 ms,bo( num @e(t ;.#( Display *at ral n m.ers s m Dim num< n< sum nG inputbo( (B'nter a ValueB) sumG) /or numG 1 to n step 1 sumG sumAnum @e(t ms,bo( sum ;.#+ Display *at ral n m.ers s m Dim num< n< sum nG inputbo( (B'nter a ValueB) :+ile num QGcdbl (n) sumG sumAnum p to n> & sing $)ile...$end 8oop' p to n> &2sing For...*e0t 8oop' p to n>
www.gcreddy.net
19
:end
I4 isnumeric (d1) G BTrueB and isnumeric (d2) G BTrueB and isnumeric (d3) G BTrueB and isnumeric (d4) G BTrueBand isnumeric (d ) G BTrueBand isnumeric (d!) G BTrueBand isnumeric (d#) G BTrueBand isnumeric (d$) G BTrueBand isnumeric (d%) G BTrueBand isnumeric (d1)) G BTrueB T+en ms,bo( BIt is a @umeric ValueB else 8s,bo( BIt is @OT @umericB
www.gcreddy.net
20
;.#: !erify weat)er t)e entered val e is a #/ digit val e or not and * meric val e or not> &2sing m ltiple if conditions' Dim a<(<y<I<num numGInputbo( (B'nter a -+one @umberB) d1G le4t (num<1) d1)G0i,+t (num<1) d2Gmid (num< 2< len d3Gmid (num< 3< len d4Gmid (num< 4< len d Gmid (num< < len d!Gmid (num< !< len d#Gmid (num< #< len d$Gmid (num< $< len d%Gmid (num< %< len
I4 len (num) G1) T+en I4 isnumeric (d1) G BTrueB and isnumeric (d2) G BTrueB and isnumeric (d3) G BTrueB and isnumeric (d4) G BTrueBand isnumeric (d ) G BTrueBand isnumeric (d!) G BTrueBand isnumeric (d#) G BTrueBand isnumeric (d$) G BTrueBand isnumeric (d%) G BTrueBand isnumeric (d1)) G BTrueB T+en ms,bo( BIt is a @umeric ValueB 'nd I4 'nd I4 I4 len (num) QR 1) T+en 8s,bo( BIt is @OT 7alid @umber B 'nd I4
www.gcreddy.net
21
T)e following looping statements are availa.le in !4Script: o o o o Do...8oop: *oops 6+ile or until a condition is True5 $)ile...$end: *oops 6+ile a condition is True5 For...*e0t: Hses a counter to run statements a speci4ied number o4 times5 For ,ac)...*e0t: 0epeats a ,roup o4 statements 4or eac+ item in a collection or eac+ element o4 an array5
C.# 2sing Do 8oops :e can use Do555*oop statements to run a bloc> o4 statements an inde4inite number o4 times5 T+e statements are repeated eit+er 6+ile a condition is True or until a condition becomes True5 C.#.# 6epeating Statements $)ile a Condition is Tr e 0epeats a bloc> o4 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 synta(F ,0ample: Dim ( Do :+ile (Q (G(A1 8s,bo( B;ello &5C50eddyB 8s,bo( B;ello 2T-B *oop b) Do Statements For "an al Testing doc ments visit:
www.gcreddy.net
22
www.gcreddy.net
23
www.gcreddy.net
24
www.gcreddy.net
25
www.gcreddy.net
26
##.- 2sing S . and F nction Proced res in Code 3 /unction in our code must al6ays be used on t+e ri,+t side o4 a 7ariable assi,nment or in an e(pression5 /or e(ampleF Temp G Celsius(4De,rees) .Or. 8s,Bo( BT+e Celsius temperature is B P Celsius(4De,rees) P B de,rees5B
www.gcreddy.net
27
www.gcreddy.net
28
Important F nctions #' A.s F nction 0eturns t+e absolute 7alue o4 a number5 Dim num numGabs(. )533) ms,bo( num (' Array F nction 0eturns a 7ariant containin, an 3rray Dim 3 3G3rray(B+yderabadB<Bc+ennaiB<BmumbaiB) ms,bo( 3()) 0eDim 3( ) 3(4)GBnelloreB ms,bo( 3(4) +' Asc F nction 0eturns t+e 3@SI c+aracter code correspondin, to t+e 4irst letter in a strin,5 Dim num numG3sc(B3B) ms,bo( num N It returns t+e 7alue ! N -' C)r F nction 0eturns t+e c+aracter associated 6it+ t+e speci4ied 3@SI c+aracter code5 Dim c+ar For "an al Testing doc ments visit:
www.gcreddy.net
29
'(2)
;' DateDiff F nction 0eturns t+e number o4 inter7als bet6een t6o dates5 Dim myday mydateGK/(@#:@(//CK 0GDatediff&LdL?mydate?*ow' msg.o0 0 C' %o r F nction 0eturns a 6+ole number bet6een ) and 23< inclusi7e< representin, t+e +our o4 t+e day5 Dim mytime< 8y+our mytimeG@o6 my+ourG+our (mytime) ms,bo( my+our #/' Moin F nction 0eturns a strin, created by ?oinin, a number o4 substrin,s contained in an array5 Dim mystrin,< myarray(3) myarray())GBC+andra B myarray(1)GB8o+an B myarray(2)GB0eddyB
www.gcreddy.net
30
##' ,val F nction '7aluates an e(pression and returns t+e result5 #(' Time F nction 0eturns a !ariant o4 subtype Date indicatin, t+e current system time5 Dim mytime mytimeGTime ms,bo( mytime #+' !arType F nction 0eturns a 7alue indicatin, t+e subtype o4 a 7ariable5 Dim 8yC+ec> 8yC+ec> G VarType(3))) 8s,bo( 8yc+ec> 9 0eturns 25 9 0eturns #5
8yC+ec> G VarType(T1)"1%"!2T) 8s,bo( 8yc+ec> 8yC+ec> G VarType(BVBScriptB) 8s,bo( 8yc+ec> #-' 8eft F nction
9 0eturns $5
Dim 8yStrin,< *e4tStrin, 8yStrin, G BVBSCriptB *e4tStrin, G *e4t(8yStrin,< 3) 9 *e4tStrin, contains BVBSB5 #-' 6ig)t F nction Dim 3nyStrin,< 8yStr 3nyStrin, G B;ello :orldB 9 De4ine strin,5 8yStr G 0i,+t(3nyStrin,< 1) 9 0eturns BdB5 8yStr G 0i,+t(3nyStrin,< !) 9 0eturns B :orldB5 8yStr G 0i,+t(3nyStrin,< 2)) 9 0eturns B;ello :orldB5 #7' 8en F nction 0eturns t+e number o4 c+aracters in a strin, or t+e number o4 bytes re1uired to store a 7ariable5 '( 1)F Dim 8ystrin, mystrin,G*en(B&5C50eddyB) ms,bo( mystrin,
www.gcreddy.net
31
www.gcreddy.net
32
(#.# Case@sensitivity: By de4ault< VBScript is not case sensiti7e and does not di44erentiate bet6een upper case and lo6er.case spellin, o4 6ords< 4or e(ample< in 7ariables< ob?ect and met+od names< or constants5 /or e(ample< t+e t6o statements belo6 are identical in VBScriptF Bro6ser(B8ercuryB)5-a,e(B/ind a /li,+tFB)5:eb*ist(BtoDayB)5Select B31B bro6ser(BmercuryB)5pa,e(B4ind a 4li,+tFB)56eblist(BtodayB)5select B31B (#.( Te0t strings: :+en 6e enter a 7alue as a te(t strin,< 6e must add 1uotation mar>s be4ore and a4ter t+e strin,5 /or e(ample< in t+e abo7e se,ment o4 script< t+e names o4 t+e :eb site< :eb pa,e< and edit bo( are all te(t strin,s surrounded by 1uotation mar>s5 @ote t+at t+e 7alue 31 is also surrounded by 1uotation mar>s< because it is a te(t strin, t+at represents a number and not a numeric 7alue5 In t+e 4ollo6in, e(ample< only t+e property name (4irst ar,ument) is a te(t strin, and is in 1uotation mar>s5 T+e second ar,ument (t+e 7alue o4 t+e property) is a 7ariable and t+ere4ore does not +a7e 1uotation mar>s5 T+e t+ird ar,ument (speci4yin, t+e timeout) is a numeric 7alue< 6+ic+ also does not need 1uotation mar>s5 Bro6ser(B8ercuryB)5-a,e(B/ind a /li,+tFB)5:ait-roperty(Bitems countB< TotalZItems< 2)))) (#.+ !aria.les: :e can speci4y 7ariables to store strin,s< inte,ers< arrays and ob?ects5 Hsin, 7ariables +elps to ma>e our script more readable and 4le(ible (#.- 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 re4lect t+e lo,ical structure and nestin, o4 t+e statements5 (#.9 Comments: For "an al Testing doc ments visit:
www.gcreddy.net
33
:e can add comments to our statements usin, an apostrop+e (9)< eit+er at t+e be,innin, o4 a separate line< or at t+e end o4 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 e(tra 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 6 n@time ,rrors VBScript run.time errors are errors t+at result 6+en our VBScript script attempts to per4orm an action t+at t+e system cannot e(ecute5 VBScript run.time errors occur 6+ile our script is bein, e(ecutedE 6+en 7ariable e(pressions 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 o4 one o4 our VBScript statements 7iolates one or more o4 t+e ,rammatical rules o4 t+e VBScript scriptin, lan,ua,e5 VBScript synta( errors occur durin, t+e pro,ram compilation sta,e< be4ore t+e pro,ram +as be,un to be e(ecuted5
www.gcreddy.net
34
www.gcreddy.net
35
www.gcreddy.net
36
Dim 4so<my4ile Set 4soGcreateob?ect(Bscriptin,54ilesystemob?ectB) Set my4ileG 4so5opente(t4ile (B/FO,cr5t(tB<1) my4ile5s>ipline :+ile my4ile5atendo4line QR True (Gmy4ile5readline sGsplit ((< B<B) SystemHtil50un BCFO-ro,ram /ilesO8ercury Interacti7eO2uic>Test -ro4essionalOsamplesO4li,+tOappO4li,+t4a5e(eB<BB<BCFO-ro,ram /ilesO8ercury Interacti7eO2uic>Test -ro4essionalOsamplesO4li,+tOappOB<BopenB
www.gcreddy.net
37
www.gcreddy.net
38
Compare/iles G /alse Do :+ile /ile153t'ndO4Stream G /alse Str1 G /ile150ead Str2 G /ile250ead Compare/iles G StrComp(Str1< Str2< )) I4 Compare/iles QR ) T+en Compare/iles G True '(it Do 'nd I4 *oop /ile15Close() /ile25Close() 'nd /unction Call Compare4iles(41<42) I4 Compare/iles(41< 42) G /alse T+en 8s,Bo( B/iles are identical5B 'lse 8s,Bo( B/iles are di44erent5B 'nd I4 A' Co nting t)e n m.er of times a word appears in a file s/ile@ameGB'FO,cr5t(tB sStrin,GB,creddyB Const /O0Z0'3DI@& G 1 Dim o/so< oT(t/ile< s0eadT(t< o0e,'(< o8atc+es Set o/so G CreateOb?ect(BScriptin,5/ileSystemOb?ectB) Set oT(t/ile G o/so5OpenTe(t/ile(s/ile@ame< /O0Z0'3DI@&) s0eadT(t G oT(t/ile50ead3ll 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(s0eadT(t) 8atc+es/ound G o8atc+es5Count Set oT(t/ile G @ot+in, F Set o/so G @ot+in, F Set o0e,'( G @ot+in, ms,bo( 8atc+es/ound
www.gcreddy.net
39
www.gcreddy.net
40
www.gcreddy.net
41
#) !erify Flig)t From & Flig)t To Com.o 4o0es (In /li,+t reser7ation< select an item 4rom /ly /romF combo bo( and 7eri4y 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 7eri4y 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) C' Jet Test Data from a Flat file and Scripting' se in Data Driven Testing &t)ro g)
#/' Jet Test Data From a Data.ase and &t)ro g) Scripting' ##'
#(' Co nt )ow many 4 ttons and ,dit .o0es availa.le in Flig)t 6eservation window> #+' !erify searc) options in 5pen 5rder Dialog .o0 &34ter selectin, open order< 3 searc+ options s+ould be enabled and not c+ec>ed< 34ter selectin, Order @o option< ot+er options s+ould be disabled< 34ter selectin, Customer @ame< /li,+t date option enabled and Order @o disabled 34ter 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' Co nt all opened 4rowsers on des3top and close all> #9' Create an ,0cel file? enter some data and save t)e file t)ro g) !4 scripting>
www.gcreddy.net
42
ww.gcreddy.com
www.gcreddy.net
43