310 likes | 408 Vues
Utils Layer. Building Library Functions. 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. What we have done up to now?.
E N D
Utils Layer Building Library Functions Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
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 ofthe “Login” dialog. • Followed the standard naming convention for all the objects. • We created two reusable actions under guiLogin Module: • guiLogin • CheckDialog Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
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 Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
Creating a function Library. • Add a new Function library. • Menu : File New Function Library • Hotkey: Press Shift + Alt + N • Toolbar as shown below: Dani Vainstein & Monika Arora Gautam
Automation FR LIB RA BL GL RS DOC DAT FR.vbs SETTING TESTS RES BATCH ENV Saving a Function Library • 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" Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
Function Definition Generator 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: Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
Function Definition Generator Function Name: FileExist fileFullPathName, By value Type: Function Scope: Public Description Documentation Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
'@Description Determine if a file exists. '@Documentation Determine if <fileFullPathName> exists. PublicFunction 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 EndFunction Modifying the function Dani Vainstein & Monika Arora Gautam
FileExists Function • To check the existence of our file ( executable ) we have to use FunctionFileExists. 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. Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
FileExists – Final Look Dani Vainstein & Monika Arora Gautam
Test Settings 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. Dani Vainstein & Monika Arora Gautam
Associating Function to Test1 Dani Vainstein & Monika Arora Gautam
Associating Function to Test1 Dani Vainstein & Monika Arora Gautam
Step Generator Using the Step Generator • Open the Step Generator. • From Menu : Insert Step Generator. • Hotkey : F7 • From menu as shown below Dani Vainstein & Monika Arora Gautam
Step Generator 1. Category: Functions 2. Library: Local script functions 3. Operation: FileExists 4. Value: C:\NotExists.txt 5. Return value : bExists Dani Vainstein & Monika Arora Gautam
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 Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
Auto Documentation ( Keyword view ) • If you don’t see the Documentation column. See next slide. Auto Documentation Column Keyword View only Determine if "C:\myFile.txt" exists. Store the result in the variable 'bExists'. Dani Vainstein & Monika Arora Gautam
Displaying Columns in Keyword view Right Click Columns headers • Or from keyword view select "View Options" ( Tools View Options ) Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
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. Dani Vainstein & Monika Arora Gautam
Make sure to visit us for: Tutorials Articles Projects And much more @ www.AdvancedQTP.com 31 Dani Vainstein & Monika Arora Gautam