basic vbscript for qtp

24
BASIC VBSCRIPT FOR QTP Basic vbscript functions which are used in QTP

Upload: cuong-tran-van

Post on 22-Apr-2015

237 views

Category:

Technology


1 download

DESCRIPTION

Basic of VBScript for QTP by Cuong Tran

TRANSCRIPT

Page 1: Basic vbscript for qtp

BASIC VBSCRIPT FOR QTPBasic vbscript functions which are used in QTP

Page 2: Basic vbscript for qtp

VBSCRIPT IN QTP

Scripting language for QuickTest Professional (QTP) is VBScript.

VBScript is a light version of Microsoft's programming language Visual Basic.

VBScript source code is contained in stand-alone files, they have the file extension .vbs.

Page 3: Basic vbscript for qtp

AGENDA

1. VBScript Variable

2. VBScript Array Variable

3. VBScript Functions and Subroutines

4. VBScript Conditional Statements

5. VBScript Looping Statements

6. VBScript Date and Time Functions

7. VBScript Built-in Functions

8. VBScript Classes

Page 4: Basic vbscript for qtp

1. VBSCRIPT VARIABLE

In VBScript all variables are of the type variant that can store any type of value.

Rules for VBScript variable name: Must begin with a letter Cannot contain period (.) Cannot exceed 255 characters

It must be distinctive (unique) within the scope in which it is declared. E.x: Dim b

b = 100

Function TEST()    Dim a,b    a = 1    b = 2    MsgBox a + bEnd Function

TESTMsgBox b

Page 5: Basic vbscript for qtp

1. VBSCRIPT VARIABLE (CONT.)

Variables can be declared explicitly and implicitly. Explicitly variables are declared with Dim

statement, Public statement, Private statement. Dim Name Dim Name, employee_address, city

Implicitly we can declare them within the script by just using the variable name. But this practice is prone to errors.

We can compel VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script.

Variable can be declared as constants (Const).

Page 6: Basic vbscript for qtp

2. VBSCRIPT ARRAY VARIABLE

Every element of an array is associated with a unique index number. By default, index number starts from 0. The number of elements in an array is a fixed number. It can also be re-adjusted dynamically.

VBScript supports up to 60 dimensions in an array. Structure:

Dim variable_name() ReDim [Preserve] variable_name(new_limit)

The first, we declare an array with no upper limit The 2nd, with ReDim we reset the upper bound to a

new value. The optional key word "Preserve" states that all of the old elements must be preserved when changing the array size.

Page 7: Basic vbscript for qtp

2. VBSCRIPT ARRAY VARIABLE (CONT.)

Example: Dim First_Array()    'dynamic array ReDim First_Array(25)    'ReDim sets the initial size of

the dynamic array to 25 ReDim First_Array(2)    'We can resize a dynamic array

unlimited number of times 'Put data to Array

First_Array(0) = “1”First_Array(1) = “2”First_Array(2) = “3”

ReDim Preserve First_Array(5)    'Resize the array, but keep the existing data    MsgBox First_Array(2)    ‘=> MsgBox displays 3

Dim arr    arr = Array(5,10,15,20)

Page 8: Basic vbscript for qtp

2. VBSCRIPT ARRAY VARIABLE (CONT.)

Some of the Array keywords and their uses:Keyword Function

Dim It will Declare an array

ReDim This is used to size or resize a dynamic array.

IsArray Will Return True if A is an array, False if it is not

LBound Will Return lower bound of an array, in VBScript it always returns 0

UBound Will Return an upper bound of array

Preserve (Optional) is used to preserve the data in an existing array when you resize it.

Erase Reinitializes the elements if it is a fixed-size array and deallocates the memory used if it is a dynamic array.

Page 9: Basic vbscript for qtp

3. VBSCRIPT FUNCTIONS AND SUBROUTINES A Sub procedure:

is a series of statements, enclosed by the Sub and End Sub statements

can perform actions, but does not return a value can take arguments

A Function procedure: is a series of statements, enclosed by the Function and End

Function statements can perform actions and can return a value can take arguments that are passed to it by a calling

procedure without arguments, must include an empty set of

parentheses () returns a value by assigning a value to its name

Page 10: Basic vbscript for qtp

3. VBSCRIPT FUNCTIONS AND SUBROUTINES (CONT.)

The main difference between a function and a subroutine is that a subroutine will do some processing of the code and then quit, while a function processes some code and then returns the result back.

Calling a Procedure:• Call a Sub • Call a Funtion

Page 11: Basic vbscript for qtp

4. VBSCRIPT CONDITIONAL STATEMENTS In VBScript we have 4 conditional statements:

If statement - executes a set of code when a condition is true If...Then...Else statement - select one of two sets of lines to

execute If...Then...ElseIf statement - select one of many sets of lines to

execute Select Case statement - select one of many sets of lines to

execute Example:

Page 12: Basic vbscript for qtp

5. VBSCRIPT LOOPING STATEMENTS

In VBScript we have four looping statements: For...Next statement - runs code a specified

number of times For Each...Next statement - runs code for each

item in a collection or each element of an array Do...Loop statement - loops while or until a

condition is true While...Wend statement – loops while a

condition is true. (recommend to do not use this statement, use the Do…Loop instead)

Example:

Page 13: Basic vbscript for qtp

5. VBSCRIPT LOOPING STATEMENTS (CONT.)

1. For … Next With the help of Step keyword, we can increase or

decrease the counter by the value specified. You can exit a For...Next statement with the Exit For

keyword.

Page 14: Basic vbscript for qtp

5. VBSCRIPT LOOPING STATEMENTS (CONT.)

2. For Each … Next A For Each...Next loop repeats a block of code for each

item in a collection, or for each element of an array. It is useful when we don’t know how many elements

are there in the dynamic array.

Page 15: Basic vbscript for qtp

5. VBSCRIPT LOOPING STATEMENTS (CONT.)

3. Do … Loop It will repeat a block of code while a condition is True or until

a condition becomes True You can exit a Do...Loop statement with the Exit Do keyword.

Page 16: Basic vbscript for qtp

5. VBSCRIPT LOOPING STATEMENTS (CONT.)

4. While … Wend While Loop is a simple loop that keeps looping while a condition is

true While...Wend loops can be nested to any level.

Each Wend matches the most recent While. The Do...Loop statement provides a more structured and flexible

way to perform looping. Should use it instead.

Page 17: Basic vbscript for qtp

6. VBSCRIPT DATE AND TIME FUNCTIONS

Date() Time()

Weekday() WeekdayName

()

Page 18: Basic vbscript for qtp

6. VBSCRIPT DATE AND TIME FUNCTIONS (CONT.)

Month() MonthName(

)

DateDiff()

Page 19: Basic vbscript for qtp

6. VBSCRIPT DATE AND TIME FUNCTIONS (CONT.)

FormatDateTime()

IsDate()

Page 20: Basic vbscript for qtp

7. VBSCRIPT BUILT-IN FUNCTIONS

Ucase() Lcase()

Trim() Ltrim() Rtrim()

StrReverse()

Page 21: Basic vbscript for qtp

7. VBSCRIPT BUILT-IN FUNCTIONS (CONT.)

Round()

Randomize()

Left() Right()

Page 22: Basic vbscript for qtp

7. VBSCRIPT BUILT-IN FUNCTIONS (CONT.)

Replace()

Mid()

Split()

Page 23: Basic vbscript for qtp

8. VBSCRIPT CLASSES

Above we have created a class (Hello_World) and an instance (MyHello_World) of that class. VBScript uses the Class...End Class statements to define the contents of the class. The property (Location) and procedure (Say_Hello) are also declared within the class.

Members within the class can be declared as private and public. Private members are only visible within the class whereas public members are accessible by any code outside of the class. Public is default.

Procedures (Sub or Function) declared Public within the class are methods of the class. Public variables serve as properties of the class.

Page 24: Basic vbscript for qtp

THANK YOU!