QTP Tutorial #21 – How to Make QTP Tests Modular and Reusable Using Actions and Function Libraries

QTP Actions and Action Properties:

Actions divide a test into logical units. This aids in highly readable tests and make sure that modularity is achieved.

There are 3 types of QTP actions:

  1. Reusable – only these can be called multiple times within the same or different tests
  2. Non-reusable – These cannot be called by any other actions
  3. External actions – a reusable action when called from another action becomes an external action in the called action.

How to use QTP Actions in  tests?

1) By default all new actions are reusable

2) Every action has its own “Action” datasheet or a local sheet

3) Call to an existing action in another test can be inserted by using the menu option: “Insert->Call to an existing action-> (Choose test/action)”

QTP Actions

4) The statement or action call inserted in the current test will be as follows:

 RunAction “Action2[Testname]”, oneiteration 

5) The action will be called as many times as the action called properties is specified for the parent action.

Action2 is a reusuable action in Test2. It is called on every iteration in the datatable of Test2.
Action1 is the action that calls Action2 in Test1.
Action1 runs only once.
Then, Action2 will run only once irrespective on how many times it was called in Test2.

6) A previously reusable action can be changed to become non-usable and vise versa.

7) When a previously reusable action is marked as non-reusable the test that calls the action fails and a warning message is displayed to the user.

8) We have seen in the previous article how parameters can be passed “To” and “From” actions.
To define these you could do one of the following:
a) Choose the action from the keyword view, right click and choose action properties.
b) While in the action in the expert view, select Edit->Action Properties from the menu.

9) The dialog box appears with General (name, description, reusable or not), Parameters(i/p or o/p), associated repositories (can associate another action’s OR) and Usedby tabs

QTP Actions

10) To send parameters to an action the general syntax is:

 RunAction “Action Name”. Oneiteration, i/p parameters, o/p parameters 

The following are some of the ways in which you can utilize the output value of an action in your test depending on your requirement:

a) Action o/p value (value returned by a called action) can be stored in a variable.

 RunAction “Action1”,OneIteration, i/pvalues, x, x being the variable 

b) Action o/p value can be stored in an environment variable.
To create an environment variable choose “File->Settings->Environment tab” , in the ‘Variable Type’ drop down choose userdefined and click + and ‘Add new environment Parameter’ – enter the name (env_Var) and an empty value and click OK.

QTP Actions

To assign the o/p value to this variable,

 RunAction “Action1”,OneIteration, i/pvalues, Environment(“env_var”) 

c) Action o/p value can be stored in a data table column.

 RunAction “Action1”, OneIteration, i/pvalue, DataTable(“A”, dtGlobalsheet) 

QTP Functions and Function Libraries:

Though there are many built in functions available in the QTP for the tester, there might still be instances when you might want to perform a specific operation and make those steps reusable. You could do that by creating user defined functions, put them in a separate file, associate it with the test and have the function name work as a keyword for that particular test.


So by definition, a function is nothing but a piece of code that performs a certain task.

Let’s create a function library now:
Use the menu option “File->New->Function Library” or “Shift+Alt+N” or click on the “New” drop down option in the menu and choose “Function library”

QTP will open up an editor, very similar to the keyword view of a test. Here you can write your code. I am going to include statements that will login a user into gmail.com page.

QTP Functions

Function gmailLogin(uname, pwd)
SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Email").Set uname
Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Passwd").SetSecure  pwd
Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebButton("Sign in").Click
End Function

Once you are done writing the code, “Save” the function library by choosing the required name and location on your computer.
Note – A function library does not get auto included in any of your tests. You are going to have to do it explicitly.

The following are the steps to associate a function library into your test:

Step #1) Open your test or create a new test

Step #2) Depending on the settings you have on your IDE you might have the resources pane already available to you. If not choose the menu option “View->Resources” and this displays the pane.

Step #3) Right click on the “Associate Function libraries”, click the “Associate Function library” option and choose the library file you created earlier.

QTP Functions

Step #4) There will be a message that asks for the tester’s confirmation before converting the path to a relative path. This is done to make sure that the test does not fail if the files are moved as long as the same hierarchy is followed. Click yes on this dialog box:

QTP Functions

Step #5) It will appear under this node from now and that is how you verify if your association has worked or not.

Step #6) Once you have associated the library make sure you save your test.

From now on if there is a statement in my test that is as follows, it will invoke the function in the library and execute the steps in there.


In the above statement you are passing constant user name and password values. But if you choose to you can pass the values from the data table too.

 gmailLogin(DataTable("SignInName", dtGlobalSheet), DataTable("GPassword", dtGlobalSheet)) 

You can have more than one function in your library files and you can have more than one library files associated to your test.


This concludes our Function libraries and Actions.  These two topics are crucial for a tester as they make all your tests more modular and reusable.

The next topic is going to be on Object repositories (OR). We will discuss how to create, associate and work with shared ORs and OR manager.

Please feel free to post your questions and comments.

Recommended reading


#1 Kedar Thorat

I’m bit confused about Actions and Functions. Can you explain the diff between these?

#2 Swati

@Kedar: Actions have their own Datatables and OR, but functions don’t. More importantly, Functions are a VBScripting element, not only applicable to QTP.

#3 Vinayak

Can you please let me know the exact diffrence between .VBS and .QFL?
When to use .VBS and when to use .QFL?

#4 shilpee naik

To associate a function library to an existing test we generally use the code as
‘Open QTP
Set objQTP = CreateObject(“QuickTest.Application”)
objQTP.Visible = True

‘Open a test and associate a function library to the test
objQTP.Open “C:\Automation\SampleTest”, False, False
What does False,False represent here???

#5 shilpee naik

To associate a function library to an existing test we generally use the code as
‘Open QTP
Set objQTP = CreateObject(“QuickTest.Application”)
objQTP.Visible = True

‘Open a test and associate a function library to the test
objQTP.Open “C:\Automation\SampleTest”, False, False
What does False,False represent here???

#6 ANA


#7 Adam

Action don’t return value like Sub-procedure, but function return value………………..

#8 shekar

to maintain action in certain order to run , please go to view menu and select the Test flow , then you will get the side Bar. in that you can see the action order . from this place you can change as the action by drag and drop as either up or Down then run your test./ thank you

#9 Naren

Diff between Action and Functions .
1. Action is local to QTP whereas Function is global.
2. Action is not re-usable whereas Function is Re-usable.
3. Action has it own OR and datatable but Function doesn’t have it own OR or datatable.
4. We can call a function in an Action but not action in a function.
5. Action can run independently but function can’t run independently

#10 Warren Moyaha

Thanks Naren atleast now i know the different.

#11 Sharaz

I need to call an external action which contain linked function library as well as data table . While calling external action , only local object repository getting linked . Any solution to call function library plus data table along with the action ? Please help me.I am stuck here

#12 Tamilarasan

In which formet,test will be perform In UFT?i mean is tests will seperate in diff actions and that actions can be reusable?

Leave a Comment