Function library in UFT is used to store commonly used functions that can be used on multiple test scripts. We should always create user-defined functions to make the automation test scripts modular, readable, and easy to maintain and it should be placed in a function library in the UFT.
What is a Function?
A function is a reusable code that should be created if we want to perform an operation, multiple times during the automation of an application.
Global Functions in UFT
A user-defined function becomes a global function if it is written in a function library.
Defining First User-Defined Function
1 2 3 4 |
Function AddNum(Byval num1,ByVal num2) SumTotal =num1+num2 Msgbox SumTotal End Function |
You can call this function in the script using the Call statement (built-in method of UFT) as shown below.
1 |
Call AddNum(10,20) 'Calling AddNum function and passing two numbers in the input parameter |
Function VS Subroutine
You can write the reusable code either in a function or in a subroutine. The difference between a function and a subroutine is that a function may or may not return a value. A subroutine never returns a value. In the example of the AddNum function, we are not returning any value instead, the output is displayed in a message box inside the function. Hence we can also write a subroutine for AddNum rather than a function.
Parameters of a Function/Subroutine
A parameter or argument is a special kind of variable that is used to provide input values to a function or a subroutine. In the example of the AddNum function as well as subroutine, both num1 and num2 are input parameters.
You can define a function or subroutine either without a parameter or one or more than one parameter as per the need.
User-defined Function Naming Rules
The function/subroutine name should never start with a number or special character. It should always start with an alpha character.
Do not use the name of the built-in function (for example, GetLastError, MsgBox, or Print). Similarly, do not use VBScript registered words (for example, CStr, F1, ESC) for function names. A list of built-in functions can be found in the Step Generator (Design > Step Generator). If you want to learn to know more about the Step Generator please refer to my post Cheat Sheet on Step Generator UFT.
How to return a value from a function?
The value of a function can be returned by assigning the value to the function name itself in the function.
1 2 3 4 |
Function AddNum(Byval num1,ByVal num2) SumTotal =num1+num2 AddNum = SumTotal End Function |
At the time of calling the function store its value in a variable and use the value wherever required in the script.
1 |
intSum = AddNum(10,20)'Calling function AddNum and storing output value in variable intSum |
Create a Function Library in UFT and associate it with a Test
Go to File>New>Function Library.
Provide the name of the function library and save it to the required location on your local machine or a server. An empty function library will be displayed. Write your functions in it and save the function library file.
Now you have to associate the function library with your Test before executing it. Go to File>Settings. The test Setting window will be displayed. Click on the + icon and browse the function library. The UFT will display a popup window as shown below.
Click the “Yes” button to use the relative path of the function library or press the “No” button to use the absolute path of the file. After that click the “OK” button followed by clicking on the “Apply” button.
You will see your function library is displaying in the solution explorer. Now you can call the user-defined function from the function library to your script without any issue.
Convert Repeatable Functionality into a Reusable Function in UFT
Let’s say we have recorded the code for doing a login to the Mercury Tour demo application.
As you can see, there are four lines of code. The last is for synchronizing the page. In the real-life actual project usually, the login function is called many times across different script which caters to different workflows/modules of the application. So, it won’t be a good approach to copy-paste the code in each Test for doing log-in. The best approach is to create a login function instead. Copy the piece of code for login and paste it inside a function say LoginToMercury written in your function library.
1 2 3 4 5 6 7 8 9 |
Function LoginToMercury 'Opening IE Browser and navigating to Mercury Tour Demo Web application SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://newtours.demoaut.com/" 'Doing login Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set "abcd1234" Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").SetSecure "5bbb002c40a30ea927f5c8ec387e80349800d3405e6f" Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 31,6 Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Sync End Function |
Now go to your Test and call the login function in your Test.
1 |
Call LoginToMercury 'Calling login function for Mercury Demo app |
If you have written any functionality inside a function library that required Test Objects from the object repository, you have to ensure the object library is associated with your Test else the script will throw an error at runtime for not finding the required test objects. If you are using descriptive programming then there won’t be any such issue.
As you can observe in the aforementioned function LoginToMercury, I have not used any parameters and hard coded the username and password in the function itself.
You can add parameters to the function in the LoginToMercury function to pass input values for username and password fields.
1 2 3 4 5 6 7 8 9 |
Function LoginToMercury(userName,Password) 'Opening IE Browser and navigating to Mercury Tour Demo Web application SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://newtours.demoaut.com/" 'Doing login Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set userName Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").Set Password Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Sync End Function |
Go to your Test and write the following code to read input parameter values from DataTable.
1 2 3 |
strUserName = DataTable.Value("UserName",dtGlobalSheet) strPassword = DataTable.Value("Password",dtGlobalSheet) Call LoginToMercury(strUserName,strPassword) 'Calling login function for Mercury Demo app |
Conclusion
In this article, we have learned about function and subroutine and how to effectively use them. We have also learned how to create a function library in the UFT and associate it with a Test.
Recommended Posts
- The Ultimate Cheat Sheet on Step Generator in UFT
- All You Need to Know about Checkpoints In UFT
- Reading and Updating MS Access Database Using ADODB in UFT
- Read, Write and Update Excel File In UFT
- Web Table Methods in UFT With Example
- DataTable in UFT One | Example of Datatable Methods
- Micro Focus UFT Tutorial – An Overview of UFT
- 5 Tips for Using External Environment Variables in UFT