UFT API How to create a C# function library

Automation Testing Published on:

Unless I missed something, there is no easy way to reuse code in Service Test 11.10. What happens if you create some custom code in a script that you want to reuse in another one? One way around this dilemma is to create your own DLL in Visual Studio and call it from Service Test. Let's take a look at a simple example on how to call a DLL from Service Test:

1. Create a simple .NET DLL. For this example I will use a .NET DLL I created that contains an Add function. The Add function takes two numbers and adds them together. For a step by step example on how to create this function check out my blog post QTP – How to Use a .NET DLL in QuickTest Professional/Functional Testing

2 .In Service Test create a new test. Navigate to the Toolbox area and click on the ‘Tests' tab.

  3. In the ‘Tests' section right click on ‘References' and click on the ‘Add Reference' menu option

4. On the ‘Add Reference' window select the ‘.Net Assembly Browser' and click the browse button to select your DLL

5. Your DLL should now appear under References:

6. Next go to the ToolBox>Miscellaneous folder and drag a Custom Code activity onto your canvas:

7. Select ‘Events' under the Property Sheet and select ‘Create a default handler' for ExecuteEvent:

8. Now the fun part – to call your DLL enter the following in the CodeActivity4_OnExecuteEvent:

//Namespace + Method name

  QTP.UI.Automation.QTP myDll = new QTP.UI.Automation.QTP();

        int sum = myDll.Add(5,5);


9. Adding all your C# code to a .NET DLL should make it easier to reuse code. Cool!

Leave a Reply

Your email address will not be published.

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