Function Library in UFT and User-Defined Functions

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

You can call this function in the script using the Call statement (built-in method of UFT) as shown below.

User-Defined Function in uft

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.

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.

At the time of calling the function store its value in a variable and use the value wherever required in the script.


Create a Function Library in UFT and associate it with a Test

Go to File>New>Function Library.

Function Library in UFT-1

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.Function-Library-UFT-0098765

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.Function-Library-UFT-0109765

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.

Now go to your Test and call the login function in your Test.

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.

Go to your Test and write the following code to read input parameter values from DataTable.



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

Leave a Reply