Вы находитесь на странице: 1из 29

Quick Test Professional - QTP

Session -4 VeriTest, Chennai.

Session-4: Agenda
QTP Scripting language Declaring variables Operators If statement overview Case statement overview For statement overview While & do While loop overview Functions overview Call Statement Procedure overview Difference between functions & procedures Creating a library file SystemUtil Object Q&A
2

QTP Scripting Language

VB Scripting language is used in QTP Not Case Sensitive

Declaring Variables

Variables can be declared explicitly in the script using 1. Dim statement 2. Public statement 3. Private statement

For example,
Dim Degrees

Operators
Arithmetic operators example: +, -, *, /

Assignment operators
example: = Comparison operators example: <, =,<>, <= Concatenation operators example: &, + Logical operators example: Not, And ,Or

If statement overview
Conditionally executes a group of statements, depending on the value of an expression. Syntax: If condition Then statements [Else elsestatements] End If Eg: OrderNo = InputBox("Enter Order Number") If OrderNo <= 0 Then Msgbox "You entered an invalid order number." Else Msgbox "You entered valid order number." End If
6

Select Case statement overview


Executes one of several groups of statements, depending on the value of an expression. SYNTAX Select Case testexpression [Case expressionlist-n [statements-n]] . . . [Case Else expressionlist-n [elsestatements-n]] End Select Arguments Testexpression: Any numeric or string expression.
7

Select Case statement overview Contd.


expressionlist-n: Required if Case appears. Delimited list of one or more expressions.

statements-n: One or more statements executed if testexpression matches any part of expressionlist-n.
elsestatements-n: One or more statements executed if test expression doesn't match any of the Case clauses.

Select Case statement overview[Contd]


Eg: Dim Color, MyVar = InputBox("Enter any color") Select Case MyVar Case "red" MsgBox The color is Red Case "green" MsgBox The color is Green Case "blue" MsgBox The color is Blue Case Else MsgBox "pick another color End Select End Sub
9

For Statement
Repeats a group of statements a specified number of times. SYNTAX For counter = start To end [Step step] [statements] [Exit For] [statements] Next Arguments Counter : Numeric variable used as a loop counter. Start : Initial value of counter. End : Final value of counter. Step : Amount counter is changed each time through the loop . If not specified, step defaults to one. The step argument can be either positive or negative.
10

For Statement(contd.)
Statements: One or more statements between For and Next that are executed the specified number of times. Eg: For I = 1 To 10 For J = 1 To 10 For K = 1 To 10 ... Next Next Next

11

DO While Loop Statement


Repeats a block of statements while a condition is True or until a condition becomes True.

Syntax:
Do [{While | Until} condition] [statements] [Exit Do] [statements] Loop

12

DO While Loop Statement Contd.


Arguments condition - Numeric or string expression that is True or False. If condition is Null, condition is treated as False. statements - One or more statements that are repeated while or until condition is True.

13

DO While Loop Example

Do While Counter < 20 Counter = Counter + 1 If Counter = 10 Then Check = False

Loop

14

WhileWend Statement
Executes a series of statements as long as a given condition is True. While condition [statements] Wend Arguments

condition
- Numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as False. statements - One or more statements executed while condition is True.

15

WhileWend Statement Example


Eg: Dim Counter Counter = 0 While Counter < 20 Counter = Counter + 1

Msgbox Counter
Wend

16

Exit Statement
Exit statement: Exits a block of Do...Loop, For...Next, Function, or Sub code. Exit Do Exit For Exit Function

Exit Sub

17

Functions Overview
Function: A name that define the behavior/ or a collection of the statement that on complete execution return some value to the caller The creation of function starts with the Function keyword and closes with End Function. SYNTAX [Public [Default] | Private] Function name [(arglist)]

[statements]
[name = expression] [Exit Function] [statements] [name = expression] End Function
18

Function Example
Example: Function MyFunction(text)

MsgBox text
End Function Call MyFunction("Hello World")

19

Call Statement
SYNTAX for Call Statement : [Call] name [argumentlist] Arguments Call - Optional keyword. If specified, you must enclose argumentlist in parentheses. For example: Call MyProc(0) name - Required. Name of the procedure to call. argumentlist

- Optional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure.

20

Procedures
Procedure : Procedure is a sub-routine and return no value SYNTAX: [Public [Default] | Private] Sub name [(arglist)] [statements]

[Exit Sub]
[statements] End Sub

21

Procedure Example
Eg: Sub CalcAndShowSalary(Hours, Salary) Dim dblResult dblResult = Hours * Salary txtResult = dblResult

End Sub
Call CalcAndShowSalary(dblHours, dblSalary)

22

Procedures Vs Functions

Function Returns a Value

Procedure Returns No Value

23

SystemUtil Object

SystemUtil object used to control applications and processes during a run session

SystemUtil.Run method

You can run any application from a specified location using a SystemUtil.Run statement
Ex: Systemutil.run"E:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","E:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"

24

Message Box and Input Box Function


Message Box Function: Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked. Syntax: MsgBox(prompt[, buttons][, title][, helpfile, context]) MsgBox Constants: vbOKOnly - Displays OK button only vbOKCancel - Displays OK and Cancel buttons. vbYesNoCancel - Displays Yes, No, and Cancel buttons. vbCritical - Displays Critical Message icon.
25

Message Box and Input Box Function Contd..


Example: MyVar = MsgBox ("Click no if you are not able to rectify ", vbyesno, "Please check ") Input Box Function: Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.

Syntax:
InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context]) Example: Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input)
26

What is library
A collection of subroutines and functions stored in one or more files, usually in compiled form, for linking with other programs.

Libraries are linked with the user's program to form a complete executable program.

27

How to create a library file

In QTP,Choose File > New > Function Library


Write functions and procedures in the library file Save the library file with the extension .vbs

28

Associating a Function Library


Open a new test or existing test. Click File > Settings > Resources tab. Click on the plus icon and add the library file. Click OK

29

Вам также может понравиться