Close

December 28, 2012

How to call a function in QTP?

How to call functions in QTP - Associate Function Librariy in QTP - QAInsights

A Function helps to build modularity and reusable framework in test automation. It saves time and effort, and lines of code in the program. In this blog post, we will see how to call a function in QTP.

What you need is simple notepad or open source editor notepad++ or even in QTP IDE will do. First, you need to analyze and identify the purpose of your function. In this tutorial, we will see how to sum up the values by sending two numbers to the function Add().

Here I am using a simple notepad to create a function. Simply copy and paste below code and save it as SumFunction.vbs

 

Public Function Add (a,b)

sum = a + b

Add = sum

End Function

Now launch QTP and copy and paste below code to your test script.

Dim a, b, sum

a = 5

b = 10

sum = Add(a, b)

MsgBox sum

Here I declared two variables a & b and assigned 5 & 10 respectively. I am calling the function Add() and displaying the sum in message box.

If you execute above code immediately, you will get error. Because I did not associate yet *.vbs file to the QTP script.

How to associate function or library files to QTP script?

  • Go to File > Settings
  • In Test Settings dialog box, click on Resources

 How to call functions in QTP - Associate Function Librariy in QTP - QAInsights

  • Click on + icon and then click on Browse button to map your function file i.e. SumFunction.vbs
  • Click on Apply and then OK.

You can check the syntax error in your *.vbs file by clicking on Check Syntax button or by pressing CTRL + F7 key in QTP IDE.

Now, execute the script, it will display the sum of a & b as shown below. How to call functions in QTP - Message Box - QAInsights

Happy Testing!

Thanks for visiting QAInsights!

We’re always posting interesting articles on QAInsights. I request you to subscribe so you don’t miss out anything.

Subscribe to our QAInsights YouTube Channel. Subscribe our free weekly newsletter or Google feeds

About NaveenKumar Namachivayam

Hi there! is a passionate and experienced Performance Test Analyst based in Chicago, USA with skill sets in Manual Testing, Test Automation, Performance Testing, and Test Estimation. He loves to learn and experiment new trends and models in Software Testing. Please stop by at his personal blogs: Excel Blog, DealsBrook, NaveenKumarN.in, and Affiliate Insights blog and Software Testing Memes.

One Comment on “How to call a function in QTP?

shals
May 26, 2016 at 9:57 pm

good one buddy!!

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *