Absent Member.
Absent Member.

Unable to create class instance

I am using QTP 9.5

I created 4 user-defined classes that provide access to some external testing framework facilities. Each of these classes resides in its own .qfl file. The 4 files are associated with my test using the Associate Function Libraries feature. There are no missing resources listed.

One of the user-defined classes is QaUtils. I have the following lines in my test:

Dim objQaUtils

Set objQaUtils = new QaUtils

The set statement causes the error:

Variable is undefined: 'QaUtils'

I am new to QTP, but it looks like the class definition is not in scope. Is there something else I need to do in my test in addition to including the class definition .qfl files as associated function libraries?
5 Replies
Absent Member.
Absent Member.

Above your classes in the qfl file create some helper functions that return a class object.

Function NewQaUtils()
Set NewQaUtils = New QAUtils
End Function

Then call it from your test:

Dim objQaUtils
Set objQaUtils = NewQaUtils

This is an annoying quirk of QTP, but the workaround works.

Eric Schumacher
Absent Member.
Absent Member.

Unfortunately, QTP is a little bugged in this regard. You can only use the "new" operator if the class is inside the script. Classes defined in outside libraries need to use the following work around.

Method 1.
Use the executfile function. On the library. This will allow you to use the "new" operator. The downside is that you will not be able to debug the classes. This in my opinion is unworkable.

ExecuteFile "MyFunctions.vbs"

Method 2.
Inside the class library do this...

Class MyClass
End Class

Public Function New_MyClass()
Set New_MyClass = new myClass
End Function

Then inside your script you can reference the class...

Set myInst = New_MyClass()

Using this method, you can debug the class just like any other code.
Absent Member.
Absent Member.

Thanks for the tips. That solves the problem. It also solves the problem of passing parameters to a VBScript constructor as I can now pass them to the 'create' function and call my own load functions to do the initilization.
Absent Member.
Absent Member.



I'm using classes in my script ,and QTP displays error message from Class_Initialize sub proc instead of going to catch method.

here is my scenario...

My .vbs file has 


Class test
    Private Sub Class_Initialize
        msgbox "inside test class"         
       Browser("title:=Welcome.*").WebElement("text:=Relevant Codes").Click  ' *QTP hangs here*
       Msgbox "This line is not printed"
    End Sub
    Private Sub Catch 'Catch
        If Err.Number = 0 Then Exit Sub
        Print "Entering test class Catch.."
        Print "Error caught: " & Err.Description  & vbNewLine
    End Sub
    Private Sub Class_Terminate : Catch 'Finally
        Print "Exiting.." 
    End Sub
End Class

Public Function testfunction
    Set testfunction=new test
    Set testfunction=Nothing
End Function


Main script :


On Error Resume Next
Dim obj,obj1
Set obj=testfunction


please help me here.


Thanks in Advance.

Absent Member.. Absent Member..
Absent Member..

You may have more luck getting answers to QTP questions if you post them on the QTP forum
[If this post solves or helps solve your issue, mark the thread as solved and give KUDOS to the author for their assistance.]

(Opinions expressed in my postings are mine alone, and do not reflect the opinions of my employer.No warranties express or implied for any solution/suggestion posted.)
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.