test automation

IntelliSense for your Custom QTP Class in 6 Steps

I have been researching this topic and I finally have a workaround which I have tested for the past few weeks with great success. The best part about this workaround is, that 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.

Create your QTP Modular/Custom/Generic Class

lass 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 = <strong>True</strong>
    End Function
&nbsp;
'#region Class Constructor &amp; Destructor
    Private Sub Class_Initialize()
        UserName = "test"
        UserRole = Global.UserRole
    End Sub
&nbsp;
    Private Sub Class_Terminate
        'code
    End Sub
&nbsp;
End Class
&nbsp;
Public LoginX: Set LoginX = New LoginClass

Converting QTP Class into VB.NET code

The only thing to note here is, that you must return values for Functions and Property Get. Also, the syntax of the Property construct differs slightly in VB.NET from VBS. Still, all you are including here are the names. You do not have to add the code from your QTP methods.

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.

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.

Registering Class using RegAsm.exe

Next, we will use the .NET assembly registration tool RegAsm.exe which 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 them. Launch it, and use CreateObject to create an instance of your class to see if it’s 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, the rest is just adding a few values to the Registry and we’re done!

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:

<strong>New Key in \ReservedObjects:</strong> LoginClass

<strong>String:</strong> ProgID,      <strong>Value</strong>: RelevantCodes.LoginClass
<strong>String:</strong> UIName,      <strong>Value</strong>: LoginX
<strong>DWord:</strong>  VisibleMode, <strong>Value</strong>: 2

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

Leave a Comment

Scroll to Top