QTP-How to Use a NET DLL in QuickTest Professional/Functional Testing

by Joe Colantonio on September 13, 2011

Post image for QTP-How to Use a NET DLL in QuickTest Professional/Functional Testing

Sometimes you need functionality that QTP does not provide. Other times you may need to interact with an existing DLL. Using a simple C# DLL, that we create, I will show how you can easily call it in QuickTest Pro.

Creating a Simple .NET DLL

Open Visual Studio (If you don't have a copy of Visual Studio you can download a free copy of Visual C# Express.) and create a new 'Class Library' project named QTP.UI.Automation:

 

 

Rename Class1 to QTP:

Next let's add a simple function that take two numbers and adds them together:

Go to the QTP.UI.Automation properties and make sure you have the following:

Make sure the 'Make assembly COM-Visible' is checked.

Go to the Build section and make sure that the 'Register for COM interop' is checked:

Build your solution (F6) and make sure there are no errors.

Call Your DLL in QuickTest Pro

Start QTP and add the following code:

Set oTestCom = CreateObject("QTP.UI.Automation.QTP")

msgbox oTestCom.Add(6,4)

QTP should return 10

Cool – that's it.

 

 

{ 17 comments… read them below or add one }

Malay September 14, 2011 at 6:34 am

May appear as a very basic question.
What is the relation between COM and DLL?

Reply

Joe Colantonio September 17, 2011 at 8:47 pm

Hi Malay – the way I understand it is the DLL contains your functions and COM (Component Object Model) enables QTP to be able to communicate with the DLL. Cool~Joe

Reply

ElizaF September 16, 2011 at 11:44 am

Thank you for this – opens up all sorts of new possibilities for work and play for me.

Reply

Joe Colantonio September 17, 2011 at 8:35 pm

ElizaF » Awesome – have fun! Cheers~Joe

Reply

Anish Pillai September 17, 2011 at 7:45 am

Hi Joe,

Good informative article.. :) :)

I have the following queries regarding this topic –

1. Where is this DLL saved. If I want to execute the same logic in some other machine, the how do I copy the DLL?
2. Can you specify any real scenarios where using this approach will be helpful?

Thanks

Reply

Uday October 5, 2011 at 11:22 am

Hi Joe,

How QTP knows that, the method exists in that dll file?

I know, you included the reference i.e “QTP.UI.Automation”, does it mean while building the solution it saves the solution reference some where and when i refere this in QTP, it looks in that location?

Regards,
Uday

Reply

Joe Colantonio October 7, 2011 at 3:24 pm

Uday » When you build the DLL in Visual Studio I believe that it automatically enters all the Method info in the registry. If you wanted to use the DLL on another machine you need to use RegAsm.exe to register it. Does that make sense? Thanks for the question! Cheers~Joe

Reply

KG October 13, 2011 at 3:36 am

Hi Joe,

Thanks for providing this interesting stuff. Can you please share the information of how Intellisense works for the custom dll.

In the above example you provided, after creating the object oTestCom, Intellisense should prompt with the available functions (in this case “Add” function)

Thanks
KG

Reply

Joe Colantonio October 16, 2011 at 4:03 pm

KG » Thanks KG! Good question – I have not tried myself yet. AdvancedQTP had an excellent article on Intellisense and COM that might help you: http://www.advancedqtp.com/2008/03/intellisense-and-com/ . Cheers~Joe

Reply

KG October 17, 2011 at 12:17 am

Thanks Joe. Also I found an interesting article on Intellisense using .NET
http://relevantcodes.com/intellisense-for-your-custom-class-in-6-steps/

Cheers
KG

Reply

udhayakumar February 8, 2012 at 6:28 am

Hi Joe,

i Added developers dll in reference to subcribe event . Finally i created the dll to use in qtp. But it doesnt give the triggered event state. what would be the problem?

Dim oEventObject
Dim pjstate1,cjstate1

Function subscribepj(pjstates)
msgbox pjstates
End Function
Function subscribecj(cjstates)
msgbox cjstates
End Function
Set pjstate1 = GetRef(“subscribepj”)
Set cjstate1 = GetRef(“subscribecj”)
Set oEventObject = DotNetFactory.CreateInstance(“JobManager.Listener”,”C:\Documents and Settings\uarumuga\My Documents\visual studio 2010\projects\JobManager\JobManager\bin\Debug\JobManager.dll”)

msgbox oEventObject.getcjstate(cjstate1)
msgbox oEventObject.getpjstate(pjstate1)

Reply

lagunascorpio May 30, 2012 at 11:03 pm

Nice one. This helped me a lot. Those COM checkboxes did the trick for me. Couldn’t find that tip anywhere else.

Reply

Joe Colantonio May 31, 2012 at 10:53 am

lagunascorpio » Great! Glad to hear it worked for you.

Reply

leon December 5, 2012 at 3:58 am

hi,
if I want use the dll in the other pc, how can i do?

Reply

Joe Colantonio December 6, 2012 at 12:51 am

leon » Hi Leon – you should be able to copy the dll over to another machine that has .net installed. Make sure to place it in the same folder location

Reply

adrian Acosta March 6, 2013 at 4:25 pm

Hi Joe, one question:
i can use description.create in visual basic .net?

Thanks¡¡

Reply

Mark April 17, 2013 at 1:58 pm

Is there a VB DLL example? instead of C#
Thanks
Mark

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: