VBScripting for QTP

  • Published on
    28-Nov-2014

  • View
    217

  • Download
    4

Embed Size (px)

Transcript

<p>VB Scripting for QTP</p> <p>1</p> <p>ContentsIntroduction ......................................................................................................................... 4 1.1 Windows Script Host (WSH).................................................................................... 4 1.2 Creating a script with Notepad ................................................................................. 4 1.3 Hosting Environments and Script Engines ............................................................... 4 1.4 Available Script Engines........................................................................................... 5 Comments ........................................................................................................................... 6 2.0 Purpose of comments: ............................................................................................... 6 2.1 Syntax ....................................................................................................................... 6 2.2 Comment/Uncomment a block of statements ........................................................... 6 2.3 Example .................................................................................................................... 6 VB Script Variables ............................................................................................................ 7 3.1 Declaring Variables .................................................................................................. 7 3.2 Option Explicit .......................................................................................................... 7 3.3 Naming Restrictions for Variables............................................................................ 7 3.4 Scope of Variables .................................................................................................... 8 3.5 Life Time of Variables .............................................................................................. 8 3.6 Assigning Values to Variables .................................................................................. 8 3.7 Scalar Variables and Array Variables ....................................................................... 8 3.8 Dynamic Arrays ........................................................................................................ 9 VB Script Data Types ....................................................................................................... 10 4.1 Variant Subtypes ..................................................................................................... 10 VB Script Operators .......................................................................................................... 11 5.1 Operator Precedence ............................................................................................... 11 5.2 Arithmetic Operators: ............................................................................................. 11 5.3 Comparison Operators ............................................................................................ 11 5.4 Concatenation Operators ......................................................................................... 12 5.5 Logical Operators.................................................................................................... 12 Input/Output Operations ................................................................................................... 13 6.1 InputBox Function .................................................................................................. 13 6.2 MsgBox Function.................................................................................................... 13 VB Script Constants .......................................................................................................... 14 7.1 Creating Constants .................................................................................................. 14 Conditional Statements ..................................................................................................... 15</p> <p>2</p> <p>8.1 Making Decisions Using If...Then...Else ................................................................ 15 8.2 Making Decisions with Select Case ........................................................................ 17 8.3 Other Examples ....................................................................................................... 18 Looping Through Code ..................................................................................................... 24 9.1 Using Do Loops ...................................................................................................... 24 9.1.1 Repeating Statements While a Condition is True ................................................ 24 9.1.2 Repeating a Statement Until a Condition Becomes True .................................... 25 9.2 While...Wend Statement ......................................................................................... 25 9.3 For...Next Statement ............................................................................................... 26 9.4 For Each...Next Statement ...................................................................................... 26 Control Flow Examples .................................................................................................... 27 VB Script Procedures ........................................................................................................ 28 12.1 Sub Procedures...................................................................................................... 28 12.2 Function Procedures.............................................................................................. 28 12.3 Procedure Arguments............................................................................................ 29 12.4 Calling the Procedures .......................................................................................... 29 VB Script Built in Functions............................................................................................. 31 VBScript syntax rules and guidelines ............................................................................... 35 Errors................................................................................................................................. 36 File System Operations ..................................................................................................... 38 I) Working with Drives and Folders ............................................................................. 38 II) Working with Flat Files ........................................................................................... 39 III) Working with Word Docs....................................................................................... 42 IV) Working with Excel Sheets .................................................................................... 42 Various Test Requirements Examples .............................................................................. 43 Solutions ........................................................................................................................... 44 Appendix A: QTP Add-Ins Information ........................................................................... 58 Appendix B: Utility Objects ............................................................................................. 68 Appendix C: Supplemental Objects .................................................................................. 69 Appendix D: VBScript Glossary ...................................................................................... 71</p> <p>3</p> <p>Introductiono o VBScript is a scripting language. VBScript is a light version of Microsoft's programming language Visual Basic.</p> <p>When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. Microsoft Visual Basic Scripting Edition brings active scripting to a wide variety of environments, including Web client scripting in Microsoft Internet Explorer and Web server scripting in Microsoft Internet Information Service.</p> <p>1.1 Windows Script Host (WSH)It is a Windows administration tool. WSH creates an environment for hosting scripts. That is, when a script arrives at your computer, WSH plays the part of the host it makes objects and services available for the script and provides a set of guidelines within which the script is executed. Among other things, Windows Script Host manages security and invokes the appropriate script engine Windows Script Host is built into Microsoft Windows 98, 2000, and Millennium Editions and higher versions. A Windows script is a text file. We can create a script with any text editor as long as we save our script with a WSH-compatible script extension (.js, vbs, or .wsf). The most commonly available text editor is already installed on our computer Notepad. We can also use your favorite HTML editor, VbsEdit, Microsoft Visual C++, or Visual InterDev.</p> <p>1.2 Creating a script with Notepad1.Start Notepad. 2.Write your script. For example purposes, type Msgbox "Hello VB Script" 3.Save this text file with a .vbs extension (instead of the default .txt extension). For example, Hello.vbs 4.Navigate to the file you just saved, and double-click it. 5.Windows Script Host invokes the VB Script engine and runs your script. In the example, a message box is displayed with the message "Hello VB Script"</p> <p>1.3 Hosting Environments and Script EnginesScripts are often embedded in Web pages, either in an HTML page (on the client side) or in an ASP page (on the server side).</p> <p>4</p> <p>In the case of a script embedded in an HTML page, the engine component that interprets and runs the script code is loaded by the Web browser, such as Internet Explorer. In the case of a script embedded in an ASP page, the engine that interprets and runs the script code is built into Internet Information Services (IIS). Windows Script Host executes scripts that exist outside an HTML or ASP page and that stand on their own as text files. 1.4 Available Script Engines Generally, we write scripts in either Microsoft JScript or VBScript, the two script engines that ship with Microsoft Windows 98, 2000 and Millennium Editions. We can use other script engines, such as Perl, REXX, and Python, with Windows Script Host. A stand-alone script written in JScript has the .js extension; a stand-alone script written in VBScript has the .vbs extension. These extensions are registered with Windows. When we run one of these types of files, Windows starts Windows Script Host, which invokes the associated script engine to interpret and run the file.</p> <p>5</p> <p>CommentsThe comment argument is the text of any comment we want to include. 2.0 Purpose of comments: o o We can use comments for making the script understandable. We can use comments for making one or more statements disable from execution.</p> <p>2.1 Syntax Rem comment (After the Rem keyword, a space is required before comment.) Or Apostrophe (') symbol before the comment 2.2 Comment/Uncomment a block of statements Select block of statement and use short cut key Ctrl + M (for comment) Select comment block and use short cut key Ctrl + Shift + M (for uncomment) 2.3 Example</p> <p>6</p> <p>VB Script VariablesA variable is a convenient placeholder that refers to a computer memory location where we can store program information that may change during the time our script is running. 3.1 Declaring Variables We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement. For example: Dim city Dim x We declare multiple variables by separating each variable name with a comma. For Example: Dim x, Top, Bottom, Left, Right We can also declare a variable implicitly by simply using its name in our script. That is not generally a good practice because we could misspell the variable name in one or more places, causing unexpected results when our script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in our script. 3.2 Option Explicit Forces explicit declaration of all variables in a script. Option Explicit ' Force explicit variable declaration. Dim MyVar ' Declare variable. MyInt = 10 ' Undeclared variable generates error. MyVar = 10 ' Declared variable does not generate error. 3.3 Naming Restrictions for Variables Variable names follow the standard rules for naming anything in VBScript. A variable name: o o o o Must begin with an alphabetic character. Cannot contain an embedded period. Must not exceed 255 characters. Must be unique in the scope in which it is declared.</p> <p>7</p> <p>3.4 Scope of Variables A variable's scope is determined by where we declare it. When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable. If we declare a variable outside a procedure, we make it recognizable to all the procedures in our script. This is a script-level variable, and it has script-level scope. 3.5 Life Time of Variables The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. 3.6 Assigning Values to Variables Values are assigned to variables creating an expression as follows: The variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example: A = 200 City = Pune X=100: Y=200 3.7 Scalar Variables and Array Variables A variable containing a single value is a scalar variable. A variable containing a series of values, is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name. Example: Dim A(3) Although the number shown in the parentheses is 3, all arrays in VBScript are zerobased, so this array actually contains 4 elements. We assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:</p> <p>8</p> <p>A(0) A(1) A(2) A(3)</p> <p>= = = =</p> <p>256 324 100 55</p> <p>Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example: SomeVariable = A(4) Arrays aren't limited to a single dimension. We can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. In the following example, the MyTable variable is a two-dimensional array consisting of...</p>