INTRODUCTION TO Function and Function Library IN

advertisement
Www.h2kinfosys.com
Introduction to Function and Function Library in UFT
INTRODUCTION TO Function and Function Library IN UFT
WHAT IS VBSCRIPT?
 VBScript is a scripting language
 a scripting language is a lightweight programming language
 VBScript is a light version of Microsoft's programming language Visual Basic
A FUNCTION:





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
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
FUNCTION LIBRARY
Management of Generic functions that are reusable, generic and non-application dependent. These
functions can be used across different functionalities and applications.
A Function Library is a collection of reusable code that is separated from the test itself and stored in an
individual file. QTP/UFT tests can then leverage this shared code without the need to repeat the logic in
each test. Effective use of function libraries is one of the biggest factors to reduce the maintenance cost
of your testing effort! A function library should be saved with the .qfl file extension, but older
implementations may use the .vbs or .txt extension.
Select ‘File -> Settings’ from the main menu
3) Select the ‘Resources’ tab
4) Click the ‘Add Resource’ button
5) Click the ‘Browse’ button
6) Spend more time browsing for the function library you want to add, and then selecting it.
7) Click ‘OK’ to confirm your changes
Creation of function libraries containing Functions which Extend QTP Functionality
By Deepa
For H2KInfosys Students only!!
Www.h2kinfosys.com
Introduction to Function and Function Library in UFT
Method 1 . qfl
File – New – Function Library –
Provide Function Library name – Hit
create
Paste function code
-------------------Function openflightapp
Dialog("Login").WinEdit("Agent
Name:").Set "dddd"
Dialog("Login").WinEdit("Password:
").Set "mercury"
Dialog("Login").WinButton("OK").Cli
ck
End Function
-------------------Save Function Library
Method2 - .txt
Open a new notepad file
Paste code
---------------------Function openflightapp
Dialog("Login").WinEdit("Agent
Name:").Set "dddd"
Dialog("Login").WinEdit("Password:
").Set "mercury"
Dialog("Login").WinButton("OK").Cli
ck
End Function
-------------------File – Save
Save notepad as filename.txt
Method3 - .vbs
Open a new notepad file
Paste code
---------------Function openflightapp
Dialog("Login").WinEdit("Agent
Name:").Set "dddd"
Dialog("Login").WinEdit("Password:
").Set "mercury"
Dialog("Login").WinButton("OK").Cli
ck
End Function
------------------File – Save as – change “Save as
Type” to All files
Provide filename as filename.vbs (
Ex openapp.vbs)
Call Function Library
1. Open a new test
2. File- Settings – Resources- Click on “+” to browse to the location where Function Library is saved
3. Select Function Library – On message .. hit yes
4. You will be able to see Function Library under toolbox
Add Object Repository – Resources – Associate Repository – Click on “..” to browse to the location
where Object Repository is saved
Select Object Repository and Hit Open
Click >> to add it to action
Hit OK
Run test
DIFFERENCE BETWEEN ACTION AND FUNCTION
Action
can only accept primitive data types as
parameters (strings, integers, etc)
Function
Can accept arrays, dictionary objects and test
objects (i.e. Pages, Frames, WebRadioGroups, etc.)
Resource Usage
For each action, there’s a folder, three
separate vbs files, a local Object
Repository, a subfolder containing
snapshots, an Excel spreadsheet, and a
few seconds of load time
There’s the code contained in the Function, and
that’s all
Functions
You cannot insert calls to Existing
Actions through the QTP API, you can
only do it through the interface
You can attach Function Libraries to Test Scripts
through the QTP API
Passing Data
By Deepa
For H2KInfosys Students only!!
Www.h2kinfosys.com
Return Values
Introduction to Function and Function Library in UFT
Return values are difficult to
understand and read in the syntax
Return values work like they do in other languages
(i.e. as lvalues)
 AND Operator (AND ,&)
Performs a logical conjunction on two expressions.
Result = expression1 And expression2
If, and only if, both expressions evaluate to True, result is True. If either expression evaluates to False,
result is False. The following table illustrates how result is determined:
If expression1 is
And expression2 is
The result is
True
True
True
True
False
False
False
True
False
False
False
False
 OR Operator (OR)
Performs a logical disjunction on two expressions.
Result = expression1 or expression2
If either or both expressions evaluate to True, result is True. The following table illustrates how result is
determined:
If expression1 is
And expression2 is
The result is
True
True
True
True
False
True
False
True
True
False
False
False
 NOT Operator (NOT)
Performs logical negation on an expression.
Result = Not expression
If expression1 is
The result is
True
False
False
True
FOR...NEXT” STATEMENT
By Deepa
For H2KInfosys Students only!!
Www.h2kinfosys.com
Introduction to Function and Function Library in UFT
A For...Next loop instructs QTP/UFT to execute one or more statements a specified number of times. It
has the following syntax:
for counter = startto end [Step step]
statement
Next
In the following example, QTP/UFT calculates the table of 15
Dim i,tbl,number
Number= 15
For i=1 to 10
tbl = number * i
MsgBox total
Next
Step Execution
Number
i
tbl=number*i
Msgbox tbl
15
15
15
15
15
15
15
15
15
15
1
2
3
4
5
6
7
8
9
10
=15*1 =15*2 =15*3 =15*4 =15*5 =15*6 =15*7 =15*8 =15*9 =15*10
15
30
45
60
75
90
105
120
135
150
“IF...T HEN...ELSE” STATEMENT
The If...Then...Else statement instructs QTP/UFT to execute a statement or a series of statements based
on specified conditions. If a condition is not fulfilled, the next elseif condition or else statement is
examined. It has the following syntax:
If condition Then
Statement
ElseIf condition2 then
Statement
Else
Statement
EndIf
Condition -Condition to be fulfilled. Ex: A>20, A=20, B<=14
Statement -Statement to be executed. Ex: a=a+10, a=a+1
By Deepa
For H2KInfosys Students only!!
Download