How to call a function in QTP?

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

 

  • 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. 

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 the Author

1 thought on “How to call a function in QTP?”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Hamster - Launch JMeter Recent Test Plans SwiftlyDownload for free
+
Share via
Copy link