How to Use Functions in QTP/UFT in 3 Easy Steps
โก Smart Summary
Functions in QTP/UFT let testers package reusable VBScript logic inside library files that any test can call, cutting duplication, easing maintenance and speeding automation development across UFT and QTP projects.

If you have segments of code that you need to use several times in your tests, you may want to create a user-defined function. By using user-defined functions, your tests are shorter, and easier to design, read, and maintain.
Your own function libraries in Micro Focus UFT can contain VBScript functions, subroutines, modules etc.
You need to follow only 3 simple steps to use a function from a library in your test.
A function library differs from an action in UFT: a function has no object repository or datasheet of its own, yet any action or test can call it once the library is associated.
Copy-pasting the same steps into every action multiplies maintenance work, because a single change then has to be repeated across every copy. A function library solves this by centralizing that logic in one place that any action or test in the project can call.
3 Simple Steps to Use a Function from a Library in UFT/QTP
Step 1) Create a new function library in UFT or QTP by selecting File > New > Function Library. It opens as a new tab in QTP.
Let’s create a very simple function that shows a message box.
Function Demo MsgBox "This is a Test Function" End Function
Whenever this function is called, a message box appears. A single file can hold multiple functions, so save the file once and reuse it across many tests. UFT saves a function library with a .qfl extension by default, though a plain .vbs file works too, as the next section explains.
Step 2) Associate the library with your test. Open File > Settings > Resources > Associate Function Library, click Add, select the function library file, and click OK.
Step 3) The last step is to call the function inside the test script. Once a library is associated, calling a function is as simple as typing its name and passing any required arguments, as shown below.
' Call the function from the associated library
Demo()
UFT locates Demo in the associated library and runs it exactly as though it were written locally inside the test.
Important Notes
- Using COM, DCOM objects you can create very advanced Functions
- In fact, many of the features provided by QTP can be coded using VBScript
- We have seen automation engineers who make it more of a VB project rather than automation project
- Our recommendation is to focus on 100% automation rather than flaunting your VB skills
Click here if the video is not accessible
Function Library File Types: .qfl vs .vbs
UFT does not force you into a single file format for a function library. The Function Library editor saves new files with a .qfl extension by default, and UFT also accepts a plain-text .vbs file created in any code editor, because a QTP/UFT function library is nothing more than ordinary VBScript stored on disk; the two formats behave identically at run time.
| ASPECT | .QFL FILE | .VBS FILE |
|---|---|---|
| Created with | UFT Function Library editor | Any text or code editor |
| Opens outside UFT | No | Yes, as plain text |
| Version control friendliness | Limited | Better, since diffs are readable |
| Association method | Same for both | Same for both |
The choice mostly comes down to team workflow rather than functionality. A one-off library built quickly inside UFT works fine as .qfl, while libraries maintained in Git, SVN or another version-control system are usually kept as .vbs files, because plain text produces far more readable diffs during code review. Both formats support the full VBScript feature set, including classes, error handling and COM object creation, and both associate with a test in exactly the same way, as the next section explains.
Renaming the file extension does not change what is inside it, so converting an existing .qfl file to .vbs, or the other way around, is just a file rename followed by re-associating the renamed file with the test.
Ways to Associate a Function Library with a Test
Step 2 above uses the Resources dialog, but UFT actually offers several ways to associate a function library with a test, and picking the right one depends on whether the association should be permanent or apply only for a single run. A test can have more than one library associated at the same time, and UFT loads them in the order they appear in the Resources list.
- Associate Function Library dialog (GUI): Open File > Settings > Resources > Associate Function Library, click Add, and select the file. UFT stores this association inside the test itself, so the library loads automatically every time the test opens. This is the method shown in Step 2 above and works well as the default choice for any library that every test in a project needs.
- LoadFunctionLibrary statement: Call LoadFunctionLibrary “path” from inside a script to attach a library at run time without touching the Resources dialog. UFT treats the library exactly like a dialog-based association for the remainder of that run, which is convenient when the library path is decided dynamically.
- ExecuteFile statement: ExecuteFile “path” runs a script file and exposes its functions to the current run, but UFT does not register the file as a tracked resource, so it never appears under Associated Function Libraries in the test settings.
- Automation Object Model (AOM): An external VBScript file can launch UFT, open a test, and add a library to Test.Settings.Resources.Libraries programmatically. Build or deployment scripts commonly use this approach to configure the same library across dozens of tests in one pass.
' Load a library at run time from inside a script LoadFunctionLibrary "C:\Automation\CommonFunctions.qfl" ' ExecuteFile works the same way, but UFT does not ' track the file as an associated resource ExecuteFile "C:\Automation\CommonFunctions.qfl"
Whichever method you choose, a library only needs to be associated once per test for the GUI and AOM methods, while LoadFunctionLibrary and ExecuteFile must run again every time the script executes, since they are ordinary VBScript statements rather than saved settings.
When a library is no longer needed, remove it from the Resources list rather than leaving it associated, since an unused library can still shadow a function with the same name that you add to a different library later.
Best Practices for Writing UFT/QTP Function Libraries
A function library only pays off if the whole team can trust and maintain it. The following habits keep a library reliable as it grows.
- Use Option Explicit: Add it as the first line of every library so UFT flags a misspelled variable instead of silently creating a new one.
- Keep function names unique: If two associated libraries define a function with the same name, UFT calls whichever one loaded last, so duplicate names cause silent, hard-to-trace bugs.
- Return values instead of hardcoding them: Accept parameters and return a result, as in the example below, so the same function serves multiple tests instead of one.
- Avoid MsgBox in production scripts: A message box pauses execution until someone clicks OK. Use Reporter.ReportEvent instead so pass, fail and warning statuses are logged straight into the results tree without stopping the run.
- Group by domain, not by convenience: Keep login functions, database functions and UI helper functions in separate files so any test can associate only what it actually needs.
- Keep libraries under version control: Store function library files in the same repository as the rest of the automation project so a rollback of the project also reverts any shared function.
Function AddNumbers(ByVal num1, ByVal num2) AddNumbers = num1 + num2 End Function Dim result result = AddNumbers(10, 20) Reporter.ReportEvent micPass, "Sum Calculation", "The result is " & result
Applying these habits early costs little, but retrofitting them into a large, unmanaged library later is far more expensive.
