qtp training session4 control structures
TRANSCRIPT
-
7/27/2019 QTP Training Session4 Control Structures
1/29
Quick Test Professional - QTPSession -4VeriTest, Chennai.
-
7/27/2019 QTP Training Session4 Control Structures
2/29
2
Session-4: Agenda
QTP Scripting languageDeclaring 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
-
7/27/2019 QTP Training Session4 Control Structures
3/29
3
QTP Scripting Language
VB Scripting language is used in QTP
Not Case Sensitive
-
7/27/2019 QTP Training Session4 Control Structures
4/29
4
Declaring Variables
Variables can be declared explicitly in the script using
1. Dim statement
2. Public statement
3. Private statement
For example,
Dim Degrees
-
7/27/2019 QTP Training Session4 Control Structures
5/29
5
Operators
Arithmetic operators
example: +, -, *, /
Assignment operators
example: =
Comparison operators
example:
-
7/27/2019 QTP Training Session4 Control Structures
6/29
6
If statement overview
Conditionally executes a group of statements, depending on the value of
an expression.
Syntax:
Ifcondition Then
statements[Else elsestatements]
End If
Eg:
OrderNo = InputBox("Enter Order Number")If OrderNo
-
7/27/2019 QTP Training Session4 Control Structures
7/29
7
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/27/2019 QTP Training Session4 Control Structures
8/29
8
Select Case statement overview Contd.
expressionlist-n: Required ifCase appears. Delimited list of one or more
expressions.
statements-n: One or more statements executed if testexpression matches anypart of expressionlist-n.
elsestatements-n: One or more statements executed if test expression doesn'tmatch any of the Case clauses.
-
7/27/2019 QTP Training Session4 Control Structures
9/29
9
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 ElseMsgBox "pick another color
End Select End Sub
-
7/27/2019 QTP Training Session4 Control Structures
10/29
10
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.
-
7/27/2019 QTP Training Session4 Control Structures
11/29
11
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
-
7/27/2019 QTP Training Session4 Control Structures
12/29
12
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]
[ExitDo]
[statements]
Loop
-
7/27/2019 QTP Training Session4 Control Structures
13/29
13
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.
-
7/27/2019 QTP Training Session4 Control Structures
14/29
14
DO While Loop Example
Do While Counter < 20
Counter = Counter + 1
If Counter = 10 Then
Check = False
Loop
-
7/27/2019 QTP Training Session4 Control Structures
15/29
15
WhileWend Statement
Executes a series of statements as long as a given condition is True.
Whilecondition
[statements]
Wend
Arguments
condition
- Numeric or string expression that evaluates to True orFalse. Ifcondition
is Null, condition is treated as False.
statements
- One or more statements executed while condition is True.
-
7/27/2019 QTP Training Session4 Control Structures
16/29
16
WhileWend Statement Example
Eg:
Dim Counter
Counter = 0
While Counter < 20
Counter = Counter + 1
Msgbox Counter
Wend
-
7/27/2019 QTP Training Session4 Control Structures
17/29
17
Exit Statement
Exit statement:
Exits a block ofDo...Loop, For...Next, Function, orSub code.
Exit Do
Exit For
Exit Function
Exit Sub
-
7/27/2019 QTP Training Session4 Control Structures
18/29
18
Functions Overview
Function: A name that define the behavior/ or a collection of the statementthat 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
-
7/27/2019 QTP Training Session4 Control Structures
19/29
19
Function Example
Example:
Function MyFunction(text)
MsgBox text
End Function
Call MyFunction("Hello World")
-
7/27/2019 QTP Training Session4 Control Structures
20/29
20
Call Statement
SYNTAX for Call Statement :
[Call] name [argumentlist]
Arguments
Call
- Optional keyword. If specified, you must enclose argumentlist inparentheses. For example: Call MyProc(0)
name
- Required. Name of the procedure to call.
argumentlist
- Optional. Comma-delimited list of variables, arrays, or expressions to passto the procedure.
-
7/27/2019 QTP Training Session4 Control Structures
21/29
21
Procedures
Procedure :
Procedure is a sub-routine and return no value
SYNTAX:
[Public [Default] | Private] Sub name [(arglist)]
[statements]
[Exit Sub]
[statements]
End Sub
-
7/27/2019 QTP Training Session4 Control Structures
22/29
22
Procedure Example
Eg:
Sub CalcAndShowSalary(Hours, Salary)
Dim dblResult
dblResult = Hours * Salary
txtResult = dblResult
End Sub
Call CalcAndShowSalary(dblHours, dblSalary)
-
7/27/2019 QTP Training Session4 Control Structures
23/29
23
Procedures Vs Functions
Function Procedure
Returns a Value Returns No Value
-
7/27/2019 QTP Training Session4 Control Structures
24/29
24
SystemUtil Object
SystemUtil object used to control applications and processes during a runsession
SystemUtil.Run method
You can run any application from a specified location using aSystemUtil.Run statement
Ex:
Systemutil.run"E:\Program Files\HP\QuickTestProfessional\samples\flight\app\flight4a.exe","","E:\ProgramFiles\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
-
7/27/2019 QTP Training Session4 Control Structures
25/29
25
Message Box and Input Box Function
Message Box Function:
Displays a message in a dialog box, waits for the user to click a button, andreturns 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.
-
7/27/2019 QTP Training Session4 Control Structures
26/29
26
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 abutton, 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)
-
7/27/2019 QTP Training Session4 Control Structures
27/29
27
What is library
A collection of subroutines and functions stored in one or more files, usuallyin compiled form, for linking with other programs.
Libraries are linked with the user's program to form a complete executableprogram.
-
7/27/2019 QTP Training Session4 Control Structures
28/29
28
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
-
7/27/2019 QTP Training Session4 Control Structures
29/29
29
Associating a Function Library
Open a new test or existing test.
Click File > Settings > Resourcestab.
Click on the plus icon and add the
library file.
Click OK