VB Script

Share Embed Donate


Short Description

Download VB Script...

Description

For more QTP Information & Scripts visit: www.gcreddy.com

Advanced QTP (In VBScript Orientation) 1) Intr Introd oduc ucti tion on 2) Comm omment ents 3) VB Scr Scrip iptt Vari Variab able les s 4) VB Scr Scrip iptt Data Data Typ Types es ) VB Scr Scrip iptt Opera Operato tors rs !) Input" Input"Out Output put Oper Operati ations ons #) Cons onstan tants $) Condit Condition ional al Stat Stateme ements nts %) &ene &enera rall 'a 'amp mple les s 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 0euirements 1#) Solutions 1$)T- dd.Ins Inormation 1%) VBScript &lossary

For Complete Docment !isit:

www.gcreddy.com

For "anal Testing docments visit:

www.gcreddy.net

1

For more QTP Information & Scripts visit: www.gcreddy.com

Introdction o

VBScript is a scriptin, lan,ua,e5

o

 scriptin, lan,ua,e is a li,+t6ei,+t pro,rammin, lan,ua,e5

o

VBScript is a li,+t 7ersion o 8icrosot9s 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 8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb ser7er scriptin, in 8icrosot Internet Inormation Ser7ice5 #.# $indows Script %ost $S%' 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+e +ost = it ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o ,uidelines 6it+in 6+ic+ t+e script is eecuted5 mon, 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 8icrosot :indo6s %$< 2< and 8illennium 'ditions and +i,+er 7ersions5  :indo6s script is a tet ile5 :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 56s)5 T+e most commonly a7ailable tet editor is already installed on our computer = @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< or Visual InterDe75 #.( Creating a script wit) *otepad 15Start @otepad5 25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script 35Sa7e t+is tet ile 6it+ a 57bs etension (instead o t+e deault 5tt etension)5 /or eample< ;ello57bs 45@a7i,ate to t+e ile 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 ;ello VB Script #.+ %osting ,nvironments and Script ,ngines

For "anal Testing docments visit:

www.gcreddy.net

2

For more QTP Information & Scripts visit: www.gcreddy.com

Introdction o

VBScript is a scriptin, lan,ua,e5

o

 scriptin, lan,ua,e is a li,+t6ei,+t pro,rammin, lan,ua,e5

o

VBScript is a li,+t 7ersion o 8icrosot9s 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 8icrosot Visual Basic Scriptin, 'dition brin,s acti7e scriptin, to a 6ide 7ariety o en7ironments< includin, :eb client scriptin, in 8icrosot Internet 'plorer and :eb ser7er scriptin, in 8icrosot Internet Inormation Ser7ice5 #.# $indows Script %ost $S%' 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+e +ost = it ma>es ob?ects and ser7ices a7ailable or t+e script and pro7ides a set o ,uidelines 6it+in 6+ic+ t+e script is eecuted5 mon, 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 8icrosot :indo6s %$< 2< and 8illennium 'ditions and +i,+er 7ersions5  :indo6s script is a tet ile5 :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 56s)5 T+e most commonly a7ailable tet editor is already installed on our computer = @otepad5 :e can also use your a7orite ;T8* editor< Vbs'dit< 8icrosot Visual CAA< or Visual InterDe75 #.( Creating a script wit) *otepad 15Start @otepad5 25:rite your script5 /or eample purposes< type 8s,bo ;ello VB Script 35Sa7e t+is tet ile 6it+ a 57bs etension (instead o t+e deault 5tt etension)5 /or eample< ;ello57bs 45@a7i,ate to t+e ile 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 ;ello VB Script #.+ %osting ,nvironments and Script ,ngines

For "anal Testing docments visit:

www.gcreddy.net

2

For more QTP Information & Scripts visit: www.gcreddy.com Scripts are oten embedded in :eb pa,es< eit+er in an ;T8* pa,e (on t+e client side) or in an S- pa,e (on t+e ser7er side)5 In t+e case o a script embedded in an ;T8* pa,e< t+e en,ine component t+at interprets and runs t+e script code is loaded by t+e :eb bro6ser< suc+ as Internet 'plorer5 In t+e case o a script embedded in an S- pa,e< t+e en,ine t+at interprets and runs t+e script code is built into Internet Inormation Ser7ices (IIS)5 :indo6s Script ;ost eecutes scripts t+at eist outside an ;T8* or S- pa,e and t+at stand on t+eir o6n as tet iles5 #.- Availale Script ,ngines &enerally< 6e 6rite scripts in eit+er 8icrosot Script or VBScript< t+e t6o script en,ines t+at s+ip 6it+ 8icrosot :indo6s %$< 2 and 8illennium 'ditions5 :e can use ot+er script en,ines< suc+ as -erl< 0'< and -yt+on< 6it+ :indo6s Script ;ost5  stand.alone script 6ritten in Script +as t+e 5?s etensionE a stand.alone script 6ritten in VBScript +as t+e 57bs etension5 T+ese etensions are re,istered 6it+ :indo6s5 :+en 6e run one o t+ese types o iles< :indo6s starts :indo6s Script ;ost< 6+ic+ in7o>es t+e associated script en,ine to interpret and run t+e ile5

For "anal Testing docments visit:

www.gcreddy.net

3

For more QTP Information & Scripts visit: www.gcreddy.com

Comments T+e comment ar,ument is t+e tet o any comment 6e 6ant to include5 (./ Prpose of comments: o

o

:e can use comments or ma>in, t+e script understandable5 understandable5 :e can use comments or ma>in, one or more statements disable rom eecution5

(.# Synta0 0em

 (ter comment  (ter

t+e 0em >ey6ord< a space is reuired beore comment5) Or

postrop+e (9) symbol beore t+e comment (.( Comment12ncomment a loc3 of statements Select bloc> o statement and use s+ort cut >ey Ctrl A 8 (or comment) Select comment bloc> and use s+ort cut >ey Ctrl A S+it A 8 (or uncomment) (.+ ,0ample

For "anal Testing docments visit:

www.gcreddy.net

4

For more QTP Information & Scripts visit: www.gcreddy.com

!4 Script !ariales  7ariable is a con7enient place+older t+at reers to a computer memory location 6+ere 6e can store pro,ram inormation 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< *et< 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 reuire eplicit declaration o all 7ariables5 T+e Option 'plicit statement s+ould be t+e irst statement in our script5 +.( 5ption ,0plicit  /orces eplicit declaration o 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 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 eceed 2 c+aracters5

For "anal Testing docments visit:

www.gcreddy.net

5

For more QTP Information & Scripts visit: www.gcreddy.com o

8ust be uniue in t+e scope in 6+ic+ it is declared5

+.- Scope of !ariales  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,niable 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 lietime o a 7ariable depends on +o6 lon, it eists5 T+e lietime o a script.le7el 7ariable etends rom t+e time it is declared until t+e time t+e script is inis+ed runnin,5 t 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 ollo6sF T+e 7ariable is on t+e let side o 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  G 2 City G J;yderabadK  G1F LG2 +. Scalar !ariales and Array !ariales  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< ecept 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 :e assi,n data to eac+ o t+e elements o t+e array usin, an inde into t+e array5 For "anal Testing docments visit:

www.gcreddy.net

6

For more QTP Information & Scripts visit: www.gcreddy.com Be,innin, at ero and endin, at 4< data can be assi,ned to t+e elements o an array as ollo6sF () (1) (2) (3)

G G G G

2! 324 1 

Similarly< t+e data can be retrie7ed rom any element usin, an inde into t+e particular array element you 6ant5 /or eampleF 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, eample< 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 sie 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 sie or number o dimensions is placed inside t+e parent+eses5 /or eampleF Dim 8yrray() 0eDim not+errray() To use a dynamic array< you must subseuently use 0eDim to determine t+e number o dimensions and t+e sie o eac+ dimension5 In t+e ollo6in, eample< 0eDim sets t+e initial sie o t+e dynamic array to 25  subseuent 0eDim statement resies t+e array to 3< but uses t+e -reser7e >ey6ord to preser7e t+e contents o t+e array as t+e resiin, ta>es place5 0eDim 8yrray(2) 0eDim -reser7e 8yrray(3)

For "anal Testing docments visit:

www.gcreddy.net

7

For more QTP Information & Scripts visit: www.gcreddy.com T+ere is no limit to t+e number o times 6e can resie 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 dierent >inds o inormation< 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 Stypes Beyond t+e simple numeric or strin, classiications< a Variant can ma>e urt+er distinctions about t+e speciic nature o numeric inormation5 /or eample< 6e can +a7e numeric inormation t+at represents a date or a time5 :+en used 6it+ ot+er date or time data< t+e result is al6ays epressed as a date or a time5 :e can also +a7e a ric+ 7ariety o numeric inormation ran,in, in sie rom Boolean 7alues to +u,e loatin,.point numbers5 T+ese dierent cate,ories o inormation 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 uninitialied5 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 25 Inte,er Contains inte,er in t+e ran,e .32es t+e place o a number or strin, and ne7er c+an,es5 .# Creating Constants :e create user.deined 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 CutoDate 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 eampleF Const priceG 1< cityG J;yderabadK< G 2#

For "anal Testing docments visit:

www.gcreddy.net

12

For more QTP Information & Scripts visit: www.gcreddy.com

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 I555T+en555'lse statement is used to e7aluate 6+et+er a condition is True or /alse and< dependin, on t+e result< to speciy 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 I555T+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 or t+e I555T+en555'lse statement5 Dim myDate myDate G 2"13"%$ I 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 o code< 6e must use t+e multiple.line (or bloc>) synta5 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  ;.#.+ 6nning Certain Statements if a Condition is Tre and 6nning 5t)ers if a Condition is False

For "anal Testing docments visit:

www.gcreddy.net

13

For more QTP Information & Scripts visit: www.gcreddy.com :e can use an I555T+en555'lse statement to deine t6o bloc>s o eecutable 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 I555T+en555'lse statement allo6s us to c+oose rom se7eral alternati7es5 ddin, 'lseI clauses epands t+e unctionality o t+e I555T+en555'lse statement so 6e can control pro,ram lo6 based on dierent 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 ,0ecting a certain loc3 of statements w)en two 1 more conditions are Tre *ested If...' 'ampleF Dim State< 0e,ion

For "anal Testing docments visit:

www.gcreddy.net

14

For more QTP Information & Scripts visit: www.gcreddy.com StateGInputbo ('nter a State) 0e,ionGInputbo ('nter a 0e,ion) 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 'nd 'nd 'nd

I  I  I  I 

;.( "a3ing Decisions wit) Select Case T+e Select Case structure pro7ides an alternati7e to I555T+en555'lseI or selecti7ely eecutin, one bloc> o statements rom amon, multiple bloc>s o statements5  Select Case statement pro7ides capability similar to t+e I555T+en555'lse statement< but it ma>es code more eicient and readable5 'ampleF Option eplicit Dim s &reater t+an or eual to ! and less t+an # < t+en ,rade is /irst c) I a7era,e mar>s &reater t+an or eual to  and less t+an ! < t+en ,rade is Second d) I a7era,e mar>s &reater t+an or eual 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 es) 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 'nd 'nd 'nd

I  I  I  I 

For "anal Testing docments visit:

www.gcreddy.net

18

For more QTP Information & Scripts visit: www.gcreddy.com ;.+.; Display 5dd nmers p to n> Dim num Dim num< n< so< myile nG inputbo ('nter any Value) numG1 /or numG 1 to n step 1 Set soG createob?ect (scriptin,5ilesystemob?ect) set myileGso5opentetile ('F,cr5tt< $< true) myile56riteline num myile5close @et ;.## Display *atral nmers in reverse order p to n> Dim num Dim ainds o procedures a7ailableE t+e S procedure and t+e Fnction procedure5 ##.# S Procedres  Sub procedure is a series o VBScript statements (enclosed by Sub and 'nd Sub statements) t+at perorm actions but don9t return a 7alue5  Sub procedure can ta>e ar,uments (constants< 7ariables< or epressions 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 SyntaF 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: ( ##.( Fnction Procedres  /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 epressions t+at are passed to it by a callin, procedure)5

For "anal Testing docments visit:

www.gcreddy.net

26

For more QTP Information & Scripts visit: www.gcreddy.com I a /unction procedure +as no ar,uments< its /unction statement must include an empty set o parent+eses5  /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 SyntaF /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 G VarType(1"1%"!2) 8s,bo 8yc+ec> 8yC+ec> G VarType(VBScript) 8s,bo 8yc+ec>

9 0eturns #5

9 0eturns $5

#-' 8eft Fnction Dim 8yStrin,< *etStrin, 8yStrin, G VBSCript *etStrin, G *et(8yStrin,< 3) 9 *etStrin, contains VBS5

#-' 6ig)t Fnction Dim nyStrin,< 8yStr nyStrin, G ;ello :orld 9 Deine strin,5 8yStr G 0i,+t(nyStrin,< 1) 9 0eturns d5 8yStr G 0i,+t(nyStrin,< !) 9 0eturns  :orld5 8yStr G 0i,+t(nyStrin,< 2) 9 0eturns ;ello :orld5 #7' 8en Fnction 0eturns t+e number o c+aracters in a strin, or t+e number o bytes reuired to store a 7ariable5 ' 1)F Dim 8ystrin, mystrin,G*en(&5C50eddy) ms,bo mystrin,

For "anal Testing docments visit:

www.gcreddy.net

31

For more QTP Information & Scripts visit: www.gcreddy.com ' 2)F Dim 8ystrin, 8ystrin,GInputbo('nter a Value) 8ystrin,G*en(8ystrin,) 8s,bo 8ystrin, #9' "id Fnction 0eturns a speciied 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 Fnction 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 @   @et 'ndTime G Timer myTimeG 'ndTime . StartTime ms,bo myTime 'nd /unction Call myTime(2) #' is*meric Fnction 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 #;' Inpto0 Fnction Displays a prompt in a dialo, bo< 6aits or t+e user to input tet or clic> a button< and returns t+e contents o t+e tet bo5 Dim Input Input G InputBo('nter your name) 8s,Bo (Lou enteredF  P Input)

For "anal Testing docments visit:

www.gcreddy.net

32

For more QTP Information & Scripts visit: www.gcreddy.com #' "sgo0 Fnction 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 Dim 8yVar 8yVar G 8s,Bo (;ello :orld< !< 8s,Bo 'ample)

!4Script synta0 rles and gidelines (#.# Case@sensitivity: By deault< VBScript is not case sensiti7e and does not dierentiate bet6een upper case and lo6er.case spellin, o 6ords< or 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(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 tet strin,< 6e must add uotation mar>s beore and ater t+e strin,5 /or eample< in t+e abo7e se,ment o script< t+e names o t+e :eb site< :eb pa,e< and edit bo are all tet 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 tet strin, t+at represents a number and not a numeric 7alue5 In t+e ollo6in, eample< only t+e property name (irst ar,ument) is a tet strin, and is in uotation mar>s5 T+e second ar,ument (t+e 7alue o t+e property) is a 7ariable and t+ereore does not +a7e uotation mar>s5 T+e t+ird ar,ument (speciyin, 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) (#.+ !ariales: :e can speciy 7ariables to store strin,s< inte,ers< arrays and ob?ects5 Hsin, 7ariables +elps to ma>e our script more readable and le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 relect t+e lo,ical structure and nestin, o t+e statements5 (#.9 Comments:

For "anal Testing docments visit:

www.gcreddy.net

33

For more QTP Information & Scripts visit: www.gcreddy.com

: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 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 perorm 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 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< beore t+e pro,ram +as be,un to be eecuted5

For "anal Testing docments visit:

www.gcreddy.net

34

For more QTP Information & Scripts visit: www.gcreddy.com

For "anal Testing docments visit:

www.gcreddy.net

35

For more QTP Information & Scripts visit: www.gcreddy.com

File System 5perations I' $or3ing wit) Drives and Folders a' Creating a Folder Option 'plicit Dim ob?/SO< ob?/older< strDirectory strDirectory G DFlo,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 availale or not? if not creating t)e folder Option 'plicit Dim ob?/SO< ob?/older< strDirectory strDirectory G DFlo,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' 6etrning 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 @et f' Jetting availale space on a Dis3 Drive Set o/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set oDri7e G o/SO5&etDri7e(CF) 8s,Bo 7ailable spaceF  P oDri7e57ailableSpace

For "anal Testing docments visit:

www.gcreddy.net

36

For more QTP Information & Scripts visit: www.gcreddy.com

II' $or3ing wit) Flat Files a' Creating a Flat File Set ob?/SO G CreateOb?ect(Scriptin,5/ileSystemOb?ect) Set ob?/ile G ob?/SO5CreateTet/ile('FScript*o,5tt) ' C)ec3ing weat)er t)e File is availale or not? if not creating t)e File strDirectoryG'F str/ileGScriptin,5tt 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?/SO5CreateTet/ile('FScript*o,5tt) '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?/SO5OpenTet/ile('F,cr5tt< 1) Do Hntil ob?/ile5t'ndOStream 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?/SO5OpenTet/ile('F,cr5tt< 1) Do Hntil ob?/ile5t'ndOStream strC+aracters G ob?/ile50eadline ms,bo strC+aracters *oop e' 6eading data from a flat file and sing in data driven testing Dim soboo>s5Open('F,cr15ls) Set ob?:or>boo>2G ob?'cel5:or>boo>s5Open('F,cr25ls) 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(cell5ddress)5Value T+en ms,bo 7alue is dierent   'lse ms,bo 7alue is same 'nd I    @et ob?:or>boo>15close ob?:or>boo>25close ob?'cel5uit set ob?'celGnot+in,

For "anal Testing docments visit:

www.gcreddy.net

40

For more QTP Information & Scripts visit: www.gcreddy.com

Test 6eNirements 1) !erify 8ogin 4ondary (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< beore enter any data ater enter data dialo, bo s+ould be disappeared5)

+' !erify Addition? Straction? "ltiplication and Division 5perations in Calclator Application.

4) !erify state of 2pdate 5rder 4tton? efore open an 5rder and after open an 5rder (in /li,+t 0eser7ation beore openin, an order Hpdate Order button s+ould be disabled ater 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) For "anal Testing docments visit:

www.gcreddy.net

41

For more QTP Information & Scripts visit: www.gcreddy.com

#) !erify Flig)t From & Flig)t To Como 4o0es (In /li,+t reser7ation< select an item rom /ly /romF combo bo and 7eriy 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 7eriy 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)rog) Scripting'

#/' Jet Test Data From a Dataase and se in Data Driven Testing t)rog) Scripting'

##'

Cont? )ow many lin3s availale in "ercry Tors %ome Page>

#(' Cont )ow many 4ttons and ,dit o0es availale 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' 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>

For Complete Docment !isit:

For "anal Testing docments visit:

www.gcreddy.net

42

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF