31
Dani Vainstein & Monika Arora Dani Vainstein & Monika Arora Gautam Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Embed Size (px)

Citation preview

Page 1: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GauDani Vainstein & Monika Arora Gautamtam

1

Utils Layer

Building Library Functions

Page 2: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 2

Topics covered

Creating a new function library file. Using the function generator. Modifying functions. Using the Step Generator. Saving a function library. Testing the functions.

Page 3: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 3

What we have done up to now? We have built an automation infrastructure. Defined QTP global options and Record & Run

settings. We created the guiLogin module. Mapped all the objects of the “Login” dialog. Followed the standard naming convention for all

the objects. We created two reusable actions under guiLogin

Module: guiLogin CheckDialog

Page 4: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 4

Before you start… Before starting the presentation, read about the

following topics in QTP help. Function library. Function Definition Generator. Expert and Keyword views. Step Generator. Functions and Subs.

ByVal, ByRef CreateObject Function. Set Statement. Nothing keyword. FileSystemObject.FileExist method. FileSystemObject.BuildPath method.

Page 5: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 5

About Function libraries

If you have set of steps that are repeated in several actions or tests, you may want to consider creating and using user-defined functions for re-usability purpose.

A user-defined function encapsulates an activity (or a group of steps that require programming) into a keyword (also called an operation).

By using user-defined functions in your tests, your tests are shorter, easier to design, read and maintain.

For more information please refer to QTP help, topic Understanding Functions and Function Libraries

Page 6: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 6

Why do we need a function library? According to the requirement Req0001a we need

to check if a file exists under a specific directory. We do not need user-interface to check the

existence of file at specific location; hence no GUI operations are required for this action.

When we do not perform GUI operations, consider to writing a function.

Of course there is many criteria's for writing functions, but this is one of them.

Page 7: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 7

Creating a function Library.

Add a new Function library. Menu : File New Function Library Hotkey: Press Shift + Alt + N Toolbar as shown below:

Page 8: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 8

Saving a Function Library

LIB

RA

TESTS

RS

DOC

FR

DAT

SETTING

RES

BATCH

ENV

Automation

BL

GL

FR.vbs

Save the function library file with the name “FR.vbs” under FR\LIB Folder

You can also save your library function with extension "qfl" or "txt"

Page 9: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 9

FileExists Function

Req0001a checks whether flightxxx.exe (executable) file exists in the installation folder

To check this we will create FileExists function. The function will receive 1 argument – the full

path of file and will return True or False value.

Page 10: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 10

Building the function

Add Option Explicit statement at the top of FR.vbs before writing the FileExists function.

For more information on Option Explicit statement please refer to the QTP help, topic : Option Explicit Statement

Create a new function From menu : Insert Function Definition Generator

From toolbar as shown below:

Function Definition Generator

Page 11: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 11

Advantages of Option Explicit Most programming languages require that you

explicitly declare variables before you use them. VBScript does not.

Some programmers think that this is an advantage, but, in fact it's a serious shortcoming.

VBScript creates a new variable in memory every time it encounters a new variable name in code.

If you misspell a name VBScript cannot know that it's an error.

This can cause serious problems and make it difficult to find the error.

Page 12: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 12

Advantages of Option Explicit It is recommended to place Option

Explicit statement in every script and function library.

Option Explicit force you to explicitly declare your variables, otherwise you'll get a run-time error.

Using Option Explicit you won't make the mistake of misspelling ( even slightly ) variable names deep in the code.

Page 13: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 13

Function Definition Generator

Function Name:

FileExist

Type: Function

Scope: Public

fileFullPathName,

By value

Description

Documentation

Page 14: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 14

FileExist Function

The Function “FileExists” that we’ve just created in the previous slide is a user-defined function.

“FileExists” is just a name, you can call it “fFileExists” or any “ExecExists”.

Don’t get confused on the FileExists method that belongs to the Scripting.FileSystemObject.

Page 15: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 15

Modifying the function

'@Description Determine if a file exists.'@Documentation Determine if <fileFullPathName> exists.

Public Function FileExists( ByVal fileFullPathName )

Dim fso

' ** Create a file system object

Set fso = CreateObject( "Scripting.FileSystemObject" )

' ** build the application path

FileExists = fso.FileExists( fileFullPathName )

Set fso = Nothing

End Function

Page 16: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 16

FileExists Function

To check the existence of our file ( executable ) we have to use Function FileExists. In order to use FileExists Method we declared fso as a Scripting.FileSystemObject variable.

For more information on FileSystemObject please refer QTP or VBScript documentation.

Then we use the Method FileExists on fileFullPathName argument and True or False value will be returned.

Page 17: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 17

FileExists Function

Just before ending the function, release the fso by setting it to Nothing e.g fso = Nothing

Although it’s not necessary, but it’s a good practice to release object variables.

Page 18: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 18

FileExists – Final Look

Page 19: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 19

Testing Your Function

Open a new Test, don’t save it. Associate the function to the newly created test

( Test1 ). From menu select : File Settings Resources tab Hotkey : and then click to resources tab. From toolbar as shown below.

Test Settings

Page 20: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 20

Associating Function to Test1

Page 21: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 21

Associating Function to Test1

Page 22: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 22

Using the Step Generator

Open the Step Generator. From Menu : Insert Step Generator. Hotkey : F7 From menu as shown below

Step Generator

Page 23: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 23

Step Generator

1. Category: Functions

2. Library:

Local script functions

3. Operation: FileExists

4. Value: C:\NotExists.txt

5. Return value : bExists

Page 24: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 24

Testing the function

Add The Print command to bExists. Add a new function call to an existing file in your

computer ( just copy paste ) Run The Script. You don’t have to save this test, it’s just for testing the

function.

bExists = FileExists( "C:\NotExists.txt" )

Print bExists

bExists = FileExists( "C:\MyFile.txt" )

Print bExists

Page 25: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 25

Documenting a Function

It’s a coding standard that helps you and your colleagues in future to know - what is the purpose of the Function/Code.

The syntax @Documentation has an advantage of “Auto-Documentation” feature of QTP. For example if you call the function in your test you can see the value of the argument under the document column of Keyword View. Let’ consider following:bExists = FileExists(“c:\MyFile.txt”)

Then you will see the result, under document column, as illustrated in next slide.

Page 26: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 26

Auto Documentation ( Keyword view )

Auto Documentation Column

Keyword View only

Determine if "C:\myFile.txt" exists. Store the result in the

variable 'bExists'.

If you don’t see the Documentation column. See next slide.

Page 27: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 27

Displaying Columns in Keyword viewRight Click Columns headers

Or from keyword view select "View Options" ( Tools View Options )

Page 28: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 28

Summary

We have created a new function Library. We add a function using the Function Definition

Generator. We’ve modified the action body to our needs. Learned about the Auto-Documentation feature. We learned how to test a function using the Step

Generator. We learned how to associate a function library to

a test.

Page 29: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 29

What’s Next?

Create a new business module. Create an initialization routine. Open an existing test. Analyzing and design requirements. Creating an Environment File. The Environment Object. Insert calls to Existing action. Passing arguments to reusable actions.

Page 30: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 30

Special Thanks To

Bharathi Babu from India, Pune. Paul Grossman from USA, Chicago. Sumit Singhal from India, Bangalore. Sanjeev Mathur from India, Noida. Prakash Kumar from India, Pune. Joydeep Das from India, Hyderabad. Richi Sharma from USA, New Jersey. Janardhan Kalvakuntla from USA, New England. Ayyappa Koppolu from India, Pune.

Page 31: Dani Vainstein & Monika Arora Gautam 1 Utils Layer Building Library Functions

Dani Vainstein & Monika Arora GautamDani Vainstein & Monika Arora Gautam 31 31

Make sure to visit us for: Tutorials Articles Projects And much more

@www.AdvancedQTP.com