IntelliSense for your Custom Class in 6 Steps

by Anshoo Arora ON June 30, 2011 · Posted In All, QTP · 24 comments

I have been using classes in my frameworks for a long time now, and my biggest gripe about QTP is its lack of Intellisense for custom classes. It also happens to be my biggest complaint. I understand this is not high priority for HP to include in its newer versions as there is a very small group of people who use OO techniques in test automation with QTP. There has been another workaround by Yaron, who used WSC to create intellisense.

I have been researching this topic and I finally have a workaround which I have tested for for the past few weeks with great success. The best part about this workaround is, I just need to create wrappers, without including any of the code I have in my QTP function libraries. If I add a new method to my class, all I do to update the Intellisense is add the method (without the QTP code) in my VB lib, create a DLL and Register the library for COM Interop.

Let’s get working then!

Step 1: Create your QTP Modular/Custom/Generic Class

Class LoginClass
 
'#region Private Variables
    Private UserName    'As String
    Private UserRole    'As String

'#region Public Variables
    Public PageTitle    'As String
    Public LinksCount   'As Integer
    
'#region Public Methods
    Public Sub CheckLinks()
        Dim arrLinks
 
        arrLinks = Array("Home", "Register", "Language", "Sign-In")
 
        Call FunctionToCheckLinks(arrLinks)
    End Sub
 
    Public Function IsPageFound() 'As Boolean
        If Browser("title:=MyApp").Exist(15) Then IsPageFound = True
    End Function
 
'#region Class Constructor & Destructor
    Private Sub Class_Initialize()
        UserName = "test"
        UserRole = Global.UserRole
    End Sub
 
    Private Sub Class_Terminate
        'code
    End Sub
 
End Class
 
Public LoginX: Set LoginX = New LoginClass

Step 2: Converting QTP Class into VB.NET code

The next step is converting the method and property names to VB.NET. If you have never used VB.NET, don’t be scared! The syntax is quite straight-forward. The only thing to note here is, you must return values for Functions and Property Get. Also, the syntax of the Property construct differs slightly in VB.NET. Still, all you are including here are the names. You do NOT have to add the code from your QTP methods.

Always remember to include the Microsoft.VisualBasic.ComClass() flag before the class, and also, remember to make the class Public

All that goes in the VB.NET code are the names of the properties and methods. Do not include any QTP code here!

Below is a conversion of the QTP code above, to VB.NET. Notice that I have not included any of the code from my QTP class here:

Save the converted code in a .VB format file
Namespace RelevantCodes
    <Microsoft.VisualBasic.ComClass()> Public Class LoginClass
 
        Public Property PageTitle As String
            Get
                return "title"
            End Get
            Set(ByVal value As String)
                'code
            End Set
        End Property
 
        Public Property LinksCount As Integer
            Get
                return 10
            End Get
            Set(ByVal value As Integer)
            End Set
        End Property
 
        Public Sub CheckLinks()
        End Sub
 
        Public Function IsPageFound() As Boolean
        End Function
 
    End Class
End Namespace

Also remember that, your Public variables become Public Properties in VB.NET.

Step 3: Creating DLL from VB.NET Class using VBC.exe

Once our class is ready and we have saved it in .vb format, let’s create the DLL using VB’s command line compiler VBC/target:library creates a .NET code library (DLL), which is what we’re looking for.

C:\windows\Microsoft.NET\Framework\v2.0.50727\vbc.exe /target:library c:\RelevantCodes.vb

Executing the above syntax in cmd.exe will create a DLL file: RelevantCodes.DLL.

The path to your VBC.exe file may be different than the one I have used.

Step 4: Registering Class using RegAsm.exe

Next, we will use the .NET assembly registration tool RegAsm.exe that reads the metadata within an assembly (which we created using VBC.exe) and adds the necessary values to your registry. RegAsm.exe syntax: regasm assemblyFile [options].

C:\windows\Microsoft.NET\Framework\v2.0.50727\regasm.exe c:\Relevantcodes.dll /codebase

If QTP is already open, save all your tests and resources with libraries and re-open it. Launch it, and use CreateObject to create an instance of your class to see if its working. The syntax will be:

Set InstanceName = CreateObject("Namespace.Class")
 
'for our class:
Set LoginX = CreateObject("RelevantCodes.LoginClass")

You must see Intellisense for the instance to ensure everything has been a success until now. If this works, rest is just adding a few values to Registry and we’re done!

Step 5: Adding the Class Reference as a QTP Reserved Word

If the above works, we’re almost done! To get the intellisense for your class, we need to navigate to the registry key below and add a few values to the new key you create. Navigate to the following key in regedit.exe:

HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\

If the above tree does not exist, try this:

HKEY_LOCAL_MACHINE\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\

Once you’re there, add a new key under reserved objects. You can give this key any name. What I generally do is, I give the same name as my QTP class. My key, then, becomes LoginClass. Once the key is created, I create the following entries in the key: ProgID (string), UIName (string) and VisibleMode (DWord).

New Key in \ReservedObjects: Name of your QTP Class

String: ProgID,      Value: Namespace.ClassName
String: UIName,      Value: Name of your reference for your custom QTP class
DWord:  VisibleMode, Value: 2

Therefore, in our case, considering the above, we will have the following values:

New Key in \ReservedObjects: LoginClass

String: ProgID,      Value: RelevantCodes.LoginClass
String: UIName,      Value: LoginX
DWord:  VisibleMode, Value: 2

The final output of adding the key and all entries to it must look like below:

Step 6: Reference the Class Instance with another Keyword

Lastly, all you need to do now is add a new variable and reference your Class Instance with it (as shown by Login below):

Public LoginX: Set LoginX = New LoginClass
Public Login : Set Login = LoginX

To create your tests and to get Intellisense, remember to associate your library with the test and use the Login keyword to see the intellisense.

I need to add a new method to my existing class. How do I do that!?!!

Well, once you converted the code for your original VB.NET library, DO NOT delete it. Once you add a new Public method to your QTP class, just add it to your VB.NET code, create (update) DLL using VBC.exe and re-register it using RegAsm. The new methods will now be available.

Summary

In summary, you have to follow the below 6 steps to create Intellisense for your custom QTP class:

  1. Create your QTP class
  2. Convert “Public” QTP methods to VB.NET (only the method names required!)
    • Public Class
    • Microsoft.VisualBasic.ComClass() attribute
  3. Use VBC.exe to create .NET library
    • Creates a DLL in the same location as the .VB file
  4. Use RegAsm.exe Assembly Registration tool to add necessary values to Registry
  5. Add the Class Instance as a Reserved word in Registry
  6. Reference the class with another Keyword

I hope you will find this helpful. As always, thanks for visiting Relevant Codes :)

A lot of time and effort has been put into this article, to research and to test it. The information here is available absolutely free of cost. If you like this article, and if it works for you, please make a donation to a charity of your choice. There is no way we will be able to check if you did, but we know you will. There is a lot of on-going crisis today, and your donation will only help. Thank you.

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Leave a Comment

{ 24 comments… read them below or add one }

Raghavendra L December 30, 2011 at 1:25 am

Anshoo,
Thanks for the detailed information. The way you explianed was very interesting.
Tarun,
Thanks for sharing the difference between VB and C#.net. I was facing exactly the same problem you have mentioned (unable to get intellisenese).

Reply

Vijesh December 6, 2011 at 1:07 am

Hi,

Has any one tried XML/SOAP Serialization using Dotnet Factory in QTP??…
When i try the below,I get an error stating ” Type ‘System.Xml.Serialization’ not found in any loaded assembly”

Set objReade2r = DotNetFactory.CreateInstance(“System.Xml.Serialization”,”System.XML”)

Please help!!

Reply

Meir October 27, 2011 at 3:44 am

Hey Anshoo,
Great article!
* I’ve got a question, though. From your example, it seems to me that the VB.NET assembly serves no other purpose than providing the intellisense effect. Am I right? If I’m correct, and disregarding QTP 12 expectations (:-), it doesn’t look to me cost effective, as you produce code (dll) that – though effectively used only at design time (and Tarun was right that this is only for dev machines) – is loaded automatically by QTP (which is already quite a memory eater).
* I think a better approach would be, from the start, to build the QTP classes as COM objects (using wsc files) and registering them as reserved objects (as you correctly pointed out), connecting them to the QTP engine through its COM object. I used this approach, for instance, to build a QTP plugin for an automation commercial tool, enabling references from QTP through the Environment object (as an example, this way the external tool could get a reference QTP report and post events or get the report status). You might refer also to Yaron Assa’s previous articles on http://www.advancedqtp.com:
1) http://www.advancedqtp.com/2008/03/reserved-objects-as-an-env-object-replacement/
2) http://www.advancedqtp.com/2008/03/intellisense-and-com/

Cheers!

Reply

Anshoo Arora December 27, 2011 at 6:36 am

Hi Meir, sorry for the late reply. To answer your question, the reason why I was searching for an alternate approach from WSC is it kept crashing QTP with both WIndows 7 x64 and Vista x32. I tried to find ways to make it work with QTP but with no concrete solution.

For the approach shown in this article, creating the DLL can be time consuming for the first time only – later on, the user only needs to edit the content of the .vb or .cs file and registering it to get the updated IntelliSense. It is however, not the best way to which I’ve agreed in the article and am myself waiting to see what comes out with QTP 12.

Thank you for your valuable input and its good to see you here :)

Reply

Prasanna October 14, 2011 at 7:03 am

Hi Anshoo,

I have built a .NET Class library (dll), which references UIAutomation Client namespaces (System.Windows.Automation). This namespace exists only in .NET frameworks 3.5 and above. Now after getting the DLL, to register it to codebase, if we use .net 2.0 RegAsm.exe, It fails to register since the reference on a namespace of 3.5 .net framework exists. There is no seperate Regasm.exe for 3.5 framework or above.

It would be great if you can assist me in this case.

Thanks,
Prasanna.

Reply

Shri October 4, 2011 at 9:40 am

Hi Anshoo,

I am a newbie to qtp. Can you have a short note which explains why we need classes in qtp?
And why do we need dlls? How exacly it helps in qtp?
I have worked primarily with qfl files only requiring procedures / functions to do the required task.(the functions doing the neeful including performing the required action on the object / performing validations etc)
If you could have a small note to explain the need for them in qtp it would be of great help.
How do we go about?
1. Create a class in a vbs file and call it in our test?
2. How do we create a dll? Do we create it in dot net and extract the methods in our qtp? How do we associate the dll to our test? and primarily why do we need to do this
Once again sorry for the elementary doubt

Reply

Boyd Patterson September 28, 2011 at 8:09 pm

This is a great little trick for getting IntelliSense for custom classes. That has always been one (of the many) flaws in the QTP IDE. I use custom classes so much that my company created an alternative IDE for use with QTP scripts called Test Design Studio. IntelliSense just works in Test Design Studio without the fancy tricks. Still nice to see a way to make QTP play nice. Maybe one day HP will give us a proper editing environment.

Until then, you can check out Test Design Studio here:

http://www.patterson-consulting.net/tds

Reply

Anshoo Arora September 29, 2011 at 10:13 pm

Boyd, thank you for your compliments and visiting Relevant Codes. I have indeed taken a look at TDS several times (from articles, snapshots) and really wish QTP did half of what I have read and learned about TDS. I have always wanted to have a tool like that (or QTP to be that), but I do hear some news about significant changes in the upcoming version of QTP and hoping HP would do something positive for people who prefer the object approach over procedural.

PS. Boyd, you have a very good blog. Love the article in response to Paul Hammant.

Reply

Boyd Patterson October 1, 2011 at 7:40 am

Thanks, Anshoo. Glad you like what you saw in TDS and for the nice comments on the blog. I like what you’re building here too. Keep it up!

Reply

Paurush September 14, 2011 at 2:47 am

How can we achive multi level intellisense?
Something like QCUtil.QCConnection.Connected
I am trying to write some classes which will call object of another another class. So what i am trying to achive is something like “CustomQCClass.Download.FromCurrentTestInstance” and “CustomQCClass.Upload.ToTestPlanFolder”

- Paurush

Reply

Anshoo Arora September 24, 2011 at 8:26 pm

Paurush: unfortunately, with the method that is shown in this method, you can only see IntelliSense up to the first level. For anything other than it, the only way I know it is possible is by creating your own assembly outside of VBScript/QTP. I suffer from this limitation already because a lot of my classes go deeper than the first level and I have not been able to find a way to achieve this. But, one thing I can tell you is, please wait for QTP 12.0 to come out. You will be surprised :)

Reply

Srinivas August 12, 2011 at 12:55 pm

Hi Anshoo,
As i am going through your post, i came to know that we can use our own custom classes from the base classes which are provide in .net. My question is that, till now we saw only creating the custom classes and using them in our local machine. Its fine till now, as my execution of scripts need to be done through QC, do i need to have those dll’s hosted in QC machine. If its correct, do we need to clarify me below questions also. 1. from whom we should get the permissions(from client or hp). 2. If i have those dlls hosted in qc and not having those dlls in my local machine where i am running my script, my scripts will work fine or not.

I came to know we can host our custom dlls by using shared libraries across machines. can we follow same to this also. Please clarify me as i am a layman to this QTP and .Net.

Hope if this works we can change all our frameworks and create an unique framework which can accomodate any kind of apps and technologies.

Regards,
Srinivas

Reply

Sunil August 3, 2011 at 4:40 pm

@ Tarun and Anshoo,

Apology for my comment.
Thanks for the clearing my doubt.

Reply

Anshoo Arora August 3, 2011 at 7:34 pm

There is no need to apologize. We always appreciate a good question :)

Reply

Sunil August 3, 2011 at 4:37 pm

@ Anshu, I got the problem from my end. Below is one of the sample code which i am using as part of my vb.net code

 Dim objWriter,XmlWriter
   Set objWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter", "System.Xml")' Creates the instance for the XMLWriter
set o = DotNetFactory.CreateInstance("using.System.Xml")

Set objread = DotNetFactory.CreateInstance("System.IO.StringWriter")
Set xmlmem =  DotNetFactory.CreateInstance("System.IO.MemoryStream")
 set XmlWriter=objWriter.Create("C:\filesystem\new folder\"& sTime & ".xml")' creates xml document

XmlWriter.WriteStartElement("Root")' start writing the element
XmlWriter.WriteStartElement("Parent")
XmlWriter.WriteAttributeString "Name", "XYZ"
XmlWriter.WriteEndElement()
 XmlWriter.WriteEndElement()'
XmlWriter.WriteEndElement()

 XmlWriter.WriteFullEndElement()

XmlWriter.close()
Set objWriter =Nothing

Sunil

Reply

Sunil August 3, 2011 at 2:08 pm

@ Tarun, You mean to say C# code written directly in QTP using dotnet factory will execute. I may not be an R&D expert like you but below are the line of code

Set vbobjWriter = DotNetFactory.CreateInstance(“System.Xml.XmlWriter”, “System.Xml”)’ Creates the instance for the XMLWriter
Set csobjWriter = DotNetFactory.CreateInstance(“using System.Xml”, “System.Xml”)’ Creates the instance for the XMLWriter

It would be great if you could point out the loop hole.

Sunil

Reply

Anshoo Arora August 3, 2011 at 2:55 pm

Sunil,

Your second statement is invalid:

Set csobjWriter = DotNetFactory.CreateInstance(“using System.Xml”, “System.Xml”)

This will not create an instance of the XMLWriter object. Regardless of how the syntax in C# and VB.Net is, their usage is similar and should also be portrayed with DOTNetFactory. Thus, you must use the following code:

Set XMLWriter = DOTNetFactory("System.Xml.XmlWriter")

'or what you write originally:
Set vbobjWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter", "System.Xml")

The below C# code

XmlWriter TheFile = XmlWriter.Create("c:\\test.xml");
TheFile.WriteComment("This is a comment!");
TheFile.WriteStartElement("StartElement");
TheFile.WriteEndElement();
TheFile.Flush();
TheFile.Close();

will be written in DOTNetFactory in the following manner:

Set TheFile = DOTNetFactory("System.Xml.XmlWriter", "System.Xml").Create("c:\\test1.xml")
TheFile.WriteComment("This is a comment!")
TheFile.WriteStartElement("StartElement")
TheFile.WriteEndElement()
TheFile.Flush()
TheFile.Close()

Reply

Tarun Lalwani August 3, 2011 at 1:40 pm

@Sunil, Yes you are wrong in saying that C# classes are not supported. I have built many classes in C# and all of them work perfectly in QTP

Reply

Sunil August 3, 2011 at 1:11 pm

Anshoo,
I have been a constant follower of your blog, Just to add to your C# code, I don’t think C# is supported after converting the code to a dll file, even if you try to use the c# code using dotnet factory you will get error. The structure of the program for C# and vb.net are different.

I have tried out the vb.net part with dotnet factory and it work perfectly, but C# is not supported.

Correct me if am wrong

Sunil

Reply

Sunil August 3, 2011 at 1:12 pm

Sorry for to mention, As it was a good post, I was struggling with importing of dll to script.

Sunil

Reply

PJ July 18, 2011 at 4:12 am

very good article ….

Reply

Tarun Lalwani July 8, 2011 at 10:53 am

“I have tried to replicate this technique using C# but I haven’t been successful. I have been able to get the correct Type for my custom class, but no success in creating Intellisense. Technically, should be the same for C#, but apparently, its not. If you find success, please share it with the community! Thank you! ”

@Anshoo, the reason this happens is the way VB.NET Compiles a class is different from way C# compiles a class. When you specify COM class in VB.NET it creates the default interface for the class and defines the interface for you. That is why you see the intellisense working. While in C# the interfaces need to be defined by you and then associated with the class.

While I do appreciate what you have done in the article. But I would like to comment that this approach should be rather used only when you the code is mature and you don’t expect much changes. This should only be done on the development machines.

Reply

Anshoo Arora July 16, 2011 at 11:50 am

As posted on SQAForums:

Anshoo:

Thanks Tarun! I saw that. I must’ve mentioned it in my thread, but because this would be required mainly on dev machines, I overlooked it instead of mentioning it.

As for the C# issue, I tried it with interface implementation before creating that article but no luck yet. I get intellisense through VS but not when I compile it using the command line compiler. Not sure why this happens. My intent was to create it for C# but ended up creating for VB.

Tarun:

I will send you a follow up on that C# issue.

Reply

Anshoo Arora August 3, 2011 at 2:58 pm

Additionally, in this article, I was not successful in showing usage of C# was because of a few settings in Windows 7 which I was doing incorrectly, which Tarun pointed out to me. I haven’t gotten a chance to compile a custom DLL after that, but I don’t see why it shouldn’t work.

Reply

Previous post:

Next post: