Going Iorward, I am starting a series oI posts on using VBScript with QTP. It will start Irom the basics oI VB Script and move to the advanced course. at is vBSciipt. v8ScrlpL ls a subseL of vlsual 8aslc 40 language lL was developed by MlcrosofL Lo provlde more processlng power Lo Web pages v8ScrlpL can be used Lo wrlLe boLh server slde and cllenL slde scrlpLlng (lf you already know vlsual 8aslc or vlsual 8aslc for AppllcaLlons (v8A) v8ScrlpL wlll be very famlllar Lven lf you do noL know vlsual 8aslc once you learn v8ScrlpL you are on your way Lo programmlng wlLh Lhe whole famlly of vlsual 8aslc languages) ata types v8ScrlpL supporLs only one daLa Lype called 'varlanL' 1he varlanL daLa Lype ls a speclal klnd of daLa Lype LhaL can conLaln dlfferenL klnds of lnformaLlon lL ls Lhe defaulL daLa Lype reLurned by all funcLlons ln v8ScrlpL A varlanL behaves as a number when lL ls used ln a numerlc conLexL and as a sLrlng when used ln a sLrlng conLexL lL ls posslble Lo make numbers behave as sLrlngs by encloslng Lhem wlLhln quoLes vaiiables A varlable ls a placeholder LhaL refers Lo a memory locaLlon LhaL sLores program lnformaLlon LhaL may change aL run Llme A varlable ls referred Lo by lLs name for accesslng Lhe value sLored or Lo modlfy lLs value vaiiable eclaiation varlables ln v8ScrlpL can be declared ln Lhree ways 1 lm SLaLemenL 2 ubllc SLaLemenL 3 rlvaLe SLaLemenL lor example lm no_assenger MulLlple varlables can be declared by separaLlng each varlable name wlLh a comma lor example lm 1op LefL 8oLLom 8lghL oo coo olso Jeclote o votloble lmpllcltly by slmply osloq lts oome lo yoot sctlpt 1bot ls oot qeoetolly o qooJ ptoctlce becoose yoo coolJ mlsspell tbe votloble oome lo ooe ot mote ploces coosloq ooexpecteJ tesolts wbeo yoot sctlpt ls too lor LhaL reason Lhe CpLlon LxpllclL sLaLemenL ls avallable Lo requlre expllclL declaraLlon of all varlables 1he CpLlon LxpllclL sLaLemenL should be Lhe flrsL sLaLemenL ln your scrlpL noLe varlables declared wlLh lm aL Lhe scrlpL level are avallable Lo all procedures wlLhln Lhe scrlpL AL Lhe procedure level varlables are avallable only wlLhln Lhe procedure ubllc sLaLemenL varlables are avallable Lo all procedures ln all scrlpLs rlvaLe sLaLemenL varlables are avallable only Lo Lhe scrlpL ln whlch Lhey are declared Nominq Convention 1here are sLandard rules for namlng varlables ln v8ScrlpL A varlable name 1 % MusL begln wlLh an alphabeLlc characLer 2 % CannoL conLaln an embedded perlod 3 % MusL noL exceed 233 characLers 4 % MusL be unlque ln Lhe scope ln whlch lL ls declared 2
__iqninq volue_ to vorioble_ values are asslgned Lo varlables creaLlng an expresslon as follows Lhe varlable ls on Lhe lefL slde of Lhe expresslon and Lhe value you wanL Lo asslgn Lo Lhe varlable ls on Lhe rlghL lor example B = 200
Scalar varlables and Array varlables
Much of Lhe Llme you only wanL Lo asslgn a slngle value Lo a varlable you have declared A varlable conLalnlng a slngle value ls a scalar varlable CLher Llmes lL ls convenlenL Lo asslgn more Lhan one relaLed value Lo a slngle varlable 1hen you can creaLe a varlable LhaL can conLaln a serles of values 1hls ls called an array varlable Array varlables and scalar varlables are declared ln Lhe same way excepL LhaL Lhe declaraLlon of an array varlable uses parenLheses ( ) followlng Lhe varlable name ln Lhe followlng example a slngledlmenslon array conLalnlng 11 elemenLs ls declared Dim A(10) AlLhough Lhe number shown ln Lhe parenLheses ls 10 all arrays ln v8ScrlpL are zerobased so Lhls array acLually conLalns 11 elemenLs ln a zerobased array Lhe number of array elemenLs ls always Lhe number shown ln parenLheses plus one 1hls klnd of array ls called a flxedslze array onstants A consLanL ls a meanlngful name LhaL Lakes Lhe place of a number or a sLrlng and never changes v8ScrlpL ln lLself has a number of deflned lnLrlnslc consLanLs llke vbCk vbCancel vb1rue vblalse and so on ?ou creaLe userdeflned consLanLs ln v8ScrlpL uslng Lhe ConsL sLaLemenL uslng Lhe ConsL sLaLemenL you can creaLe sLrlng or numerlc consLanLs wlLh meanlngful names and asslgn Lhem llLeral values lor example Const MyString = "This is my string."Const MyAge = 49 noLe LhaL Lhe sLrlng llLeral ls enclosed ln quoLaLlon marks ( ) Also noLe LhaL consLanLs are publlc by defaulL WlLhln procedures consLanLs are always prlvaLe Lhelr vlslblllLy canL be changed nexL posL we wlll deal wlLh consLrucLs and arrays onditional onstructs onditional onstructs execute statements or repeat certain set oI statements based on conditions. The Iollowing conditional constructs are available in VBScript II Then Else Select ase X - Tben - Fl_e Con_truct 1he lf 1hen Llse ConsLrucL ls used Lo evaluaLe wheLher a condlLlon ls Lrue or false and dependlng on Lhe resulL Lo speclfy one or more sLaLemenLs Lo execuLe usually Lhe condlLlon ls an expresslon LhaL uses a comparlson operaLor Lo compare one value or varlable wlLh anoLher 1he lf 1hen Llse sLaLemenLs can be nesLed Lo as many levels as needed lor example Sub ReportValue(value)If value = 0 ThenMsgBox valueElseIf value = 1 ThenMsgBox valueElseIf value = 2 thenMsgbox valueElseMsgbox "Value out of range!"End If
?ou can add as many Llself clauses as you need Lo provlde alLernaLlve cholces LxLenslve use of Lhe Llself clauses ofLen becomes cumbersome A beLLer way Lo choose beLween several alLernaLlves ls Lhe SelecL Case sLaLemenL
SelecL Case ConsLrucL
1he SelecLCase sLrucLure ls an alLernaLlve Lo lf 1hen Llse for selecLlvely execuLlng one block of sLaLemenLs from among mulLlple blocks of sLaLemenLs 1he SelecL Case ConsLrucL makes code more efflclenL and readable
A SelecL Case sLrucLure works wlLh a slngle LesL expresslon LhaL ls evaluaLed once aL Lhe Lop of Lhe sLrucLure 1he resulL of Lhe 3
expresslon ls Lhen compared wlLh Lhe values for each Case ln Lhe sLrucLure lf Lhere ls a maLch Lhe block of sLaLemenLs assoclaLed wlLh LhaL Case ls execuLed
lor example
SelecL Case ocumenLlorm1Card1ypeCpLlons(SelecLedlndex)1exL
Case MasLerCard
lsplayMCLogo
valldaLeMCAccounL
Case vlsa
lsplayvlsaLogo
valldaLevlsaAccounL
Case Amerlcan Lxpress
lsplayAMLxCCLogo
valldaLeAMLxCCAccounL
Case Llse lsplayunknownlmage rompLAgaln
Lnd SelecL
lLeraLlve ConsLrucLs Looplng allows Lo run a group of sLaLemenLs repeaLedly 1he loop ls repeaLed based on a condlLlon 1he loop runs as long as Lhe condlLlon ls Lrue 1he followlng looplng consLrucLs are avallable ln v8ScrlpL % o Loop
% Whlle Wend
% lor nexL o - loop o Loop sLaLemenLs are used Lo execuLe a block of sLaLemenLs based on a condlLlon 1he sLaLemenLs are repeaLed elLher whlle a condlLlon ls Lrue or unLll a condlLlon becomes Lrue Whlle keyword can be used Lo check a condlLlon ln a o Loop consLrucL 1he condlLlon can be checked before enLerlng lnLo Lhe loop or afLer Lhe loop has run aL leasL once 1he baslc dlfference beLween a o whlle Loop" and o Loop whlle" ls LhaL Lhe prevlous one geLs execuLed only when Lhe condlLlon ln Lhe whlle sLaLemenL holds Lrue where as a o Loop whlle" geLs execuLed aLleasL once because Lhe condlLlon ln Lhe whlle sLaLemenL geLs checked aL Lhe end of Lhe flrsL lLeraLlon Whlle Wend
1he WhlleWend sLaLemenL ls provlded ln v8ScrlpL for Lhose who are famlllar wlLh lLs usage Powever because of Lhe lack of flexlblllLy ln whllewend lL ls recommended LhaL you use oLoop lnsLead 4
or..Next 1he lornexL loop can be used Lo run a block of sLaLemenLs a speclflc number of Llmes lor loops use a counLer varlable whose value ls lncreased or decreased wlLh each repeLlLlon of Lhe loop 1he SLep keyword ls used Lo lncrease or decrease Lhe counLer varlable by Lhe value LhaL ls speclfled along wlLh lL 1he lornexL sLaLemenL can be LermlnaLed before Lhe counLer reaches lLs end value by uslng Lhe LxlL lor sLaLemenL lor example lm [ LoLal lor [ 2 1o 10 SLep 2
LoLal LoLal + [
nexL Msg8ox 1he LoLal ls LoLal Arrays
An array ls a conLlguous area ln Lhe memory referred Lo by a common name lL ls a serles of varlables havlng Lhe same daLa Lype Arrays are used Lo sLore relaLed daLa values v8ScrlpL allows you Lo sLore a group of common values LogeLher ln Lhe same locaLlon 1hese values can be accessed wlLh Lhelr reference numbers
An array ls made up of Lwo parLs Lhe array name and Lhe array subscrlpL 1he subscrlpL lndlcaLes Lhe hlghesL lndex value for Lhe elemenLs wlLhln Lhe array Lach elemenL of an array has a unlque ldenLlfylng lndex number by whlch lL can be referenced v8ScrlpL creaLes zero based arrays where Lhe flrsL elemenL of Lhe array has an lndex value of zero eclorinq rroy_ An array musL be declared before lL can be used ependlng upon Lhe accesslblllLy arrays are of Lwo Lypes % Local Arrays
A local array ls avallable only wlLhln Lhe funcLlon or procedure where lL ls declared
% Clobal Arrays
A global array ls an array LhaL can be used by all funcLlons and procedures lL ls declared aL Lhe beglnnlng of Lhe v8ScrlpL Code
1he lm sLaLemenL ls used Lo declare arrays 1he synLax for declarlng an array ls as follows
lm Arrayname(subscrlpLvalue)
Where Arrayname ls Lhe unlque name for Lhe array and SubscrlpLvalue ls a numerlc value LhaL lndlcaLes Lhe number of elemenLs ln Lhe array dlmenslon wlLhln Lhe array
Lxample
lm no_assengers(3)
1he no_assengers can sLore 4 values ssigning values to te aiiay no_assengers(0) 1 no_assengers(1) 2
no_assengers(2) 3 3
no_assengers(3) 4 SLaLlc and ynamlc Arrays v8ScrlpL provldes flexlblllLy for declarlng arrays as sLaLlc or dynamlc
A sLaLlc array has a speclflc number of elemenLs 1he slze of a sLaLlc array cannoL be alLered aL run Llme
A dynamlc array can be reslzed aL any Llme ynamlc arrays are useful when slze of Lhe array cannoL be deLermlned 1he array slze can be changed aL run Llme
In VBScript, there are two types oI procedures: 1 Sub rocedures 2 luncLlon rocedures ub ProceJure_ sub procedure is a series oI VBScript statements, enclosed by Sub and End Sub statements which perIorm actions but do not return a value. sub procedure can take arguments. II a sub procedure doesn`t receive any arguments, its Sub statement must include an empty parenthesis(). The Iollowing Sub procedure uses two intrinsic, or built-in, VBScript Iunctions, MsgBox and InputBox , to prompt a user Ior inIormation. It then displays the results oI a calculation based on that inIormation. The calculation is perIormed in a Function procedure created using VBScript. The Function procedure is shown aIter the Iollowing discussion.
Sub onvertTemp() temp InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & elsius(temp) & " degrees ." End Sub unction ProceJure_ Iunction procedure is a series oI VBScript statements enclosed by the Function and End Function statements. Iunction procedure is similar to a sub procedure but it can return value to the calling Iunction. Iunction procedure can take arguments (constants, variables or expressions that are passed to it by a calling procedure). II a Iunction procedure has no arguments, it Function statement must include an empty set oI parenthesis. Iunction returns a value by assigning a value to its name in one or more statements oI the procedure. Since VBScript has only one base data type, a Iunction always returns a variant. In the Iollowing example, the elsius Iunction calculates degrees elsius Irom degrees Fahrenheit. When the Iunction is called Irom the onvertTemp Sub procedure, a variable containing the argument value is passed to the Iunction. The result oI the calculation is returned to the calling procedure and displayed in a message box. Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub
Function Celsius(fDegrees) Celsius = (fDegrees - 32) 5 / 9 End Function
1lps 1 1o geL daLa ouL of a procedure you musL use a luncLlon 8emember a luncLlon procedure can reLurn a value a Sub procedure canL 2 A luncLlon ln your code musL always be used on Lhe rlghL slde of a varlable asslgnmenL or ln an expresslon 3 1o call a Sub procedure from anoLher procedure Lype Lhe name of Lhe procedure along wlLh values for any requlred argumenLs each separaLed by a comma 1he Call sLaLemenL ls noL requlred buL lf you do use lL you musL enclose any argumenLs ln parenLheses 4 1he followlng example shows Lwo calls Lo Lhe MyProc procedure Cne uses Lhe Call sLaLemenL ln Lhe code Lhe oLher doesnL 8oLh do exacLly Lhe same Lhlng Call Myroc(flrsLarg secondarg)
Myroc flrsLarg secondarg
noLlce LhaL Lhe parenLheses are omlLLed ln Lhe call when Lhe Call sLaLemenL lsnL used