Starting today, Saket is kicking off a series of posts on the DotNetFactory object in QTP. Saket is a senior specialist in a multi-national corporation (Yardi Software India) and a moderator at QTP Forums. He has over five years of experience in development/testing and has done extensive regression testing with QTP. In his free time he researches new IT technologies, reads books and can be found at QTP Forums replying to queries.

Saket will start with the basics of DotNetFactory. In the coming weeks he will dig deeper to take you through its advanced features and show you all that can be accomplished using this object.

DotNetFactory and QTP Part-1

DotNetFactory is a new utility object introduced in QTP 9.2 and available in later versions, which enables QTP scripting to directly access methods and properties of a .NET object by creating an instance of this object. You will be amazed using this as you won’t realize how easy it could be.

(.Net Object – A .Net object is a set of robust and well featured .Net components for creating methods and properties for different operations)

According to QTP help:

You can use this object to access the static methods and properties of a class that does not have an instance constructor, for example, System.Environment, as well as a class that does.

This utility object enables QTP scripts to work on user created forms, controls and other .Net objects. We will discuss on how to use these objects and create forms later in this series.

If you are thinking that the DOTNetFactory utility object is something which can be used to work with .Net applications only, then this is the time you change your mind. Now you will find that this can be used in your QTP script to achieve your various scripting task much easily. Also the .Net addin is not required for this to work. I personally feel that the utility has been introduced to resolve the problem that used to occur when referencing a .Net Assembly or C/C++ Dll and while making a call to its functions.

Earlier when you need to work with .Net DLLs, it was required to have the DLL compiled in a way that it could be used as a COM object. This could be used in the QTP script by creating an instance of the object with CreateObject. The problem arises, when you need to pass a .Net object to one of the arguments. It is now possible to more directly with an assembly.

The benefits doesn’t end there, you will see that things get much easier and straight-forward using utility when compared to normal VB Scripting.

(No need to worry if you don’t have much hands on with .Net technology and its objects. I will try to go through most of the .Net Objects and its usage with DotNetFactory in QTP script.)

Following example will give a better idea here.

(Example inspired from Book Scripting QTP, by Dani Vanistein. Chapter -14)

Consider a case where you need to change the format of time given as – “Fri, 9 Oct 2009” into “dd/mm/yyyy” format. Since there is no direct method available to do such an operation, you will definitely spend lots of time and have to come up with a complex function which will go through loops and conditions to get the required format.

This is now possible with the help of DotNetFactory in very simple and easy steps. See the code below

Example 1

Dim SystemDate , oDate
Set SystemDate = Dotnetfactory.CreateInstance("System.DateTime")
Set oDate = SystemDate.Parse("Fri, 9 Oct 2009")

FormattedDate = oDate.Day & "/" & oDate.Month & "/" & oDate.Year

msgbox FormattedDate

Set SystemDate = Nothing
Set oDate = Nothing

This gives you the required output as “9/10/2009”.

To use DotNetFactory, you will first need to create the object instance. To create the object instance use ‘CreateInstance’ method.

DotNetFactory.CreateInstance (TypeName [,Assembly] [,args])

TypeName – The Full Name of the object Type. E.g. System.DateTime in the above example

Assembly – This is optional. You need to pass the assembly for type. If the assembly is preloaded in the registry, you do not need to enter it. If you do not pass this argument, QTP assumes that the assembly is resident in memory. If QuickTest does not find the assembly, an error message is displayed during the run session.

Args – This is also optional, You need to pass the arguments for typename you specified or for assembly (if any)

(An assembly is the standard for components developed with Microsoft .Net framework. It’s a kind of partially compiled code which is used for deploying,security and versioning. Assemblies can be of two types, – process assemblies (EXE – executable) and library assemblies (DLL – non executable). A library assembly contains classes and its functions and the process assemblies use these classes.)

Let us see more usage of DotNetFactory in QTP. We will now create a simple custom message-box to understand it better.

In .Net ’MessageBox’ is a part of ‘System.Windows.Forms’ namespace.

(Namespace – Namespace is a way of grouping type names and reducing the chance of name collisions. It is another method that compliments assemblies. If you have some basic hands on .Net then it could be easily understandable)

When creating the instance of Message box using CreateInstance we will use ‘System.Windows.Forms. MessageBox’ as type name and ‘System.Windows.Forms’ as assembly. We can use Set statement to retrieve its COM interface.

Set MyMsgBox = DotNetFactory.CreateInstance("System.Windows.Forms.MessageBox","System.Windows.Forms")

(COM Interface – COM (Component Object Model) Interface provides the access to methods and properties of an object. This is same as we do normally using CreateObject)

Using above Set statement in the script creates the message box. This will now be displayed with a message.

After creating the COM Interface, you can use its different properties and methods. Here we have ‘Show’ method to display this message box. We will pass the message as argument for this.

MyMsgBox.Show “!! Hello World !!”, “My Custom Message box”

The first argument is the Message and the second argument is the Title for your custom message box.

Below is the complete code for the example

Example 2

Set MyMsgBox = DotNetFactory.CreateInstance("System.Windows.Forms.MessageBox",

MyMsgBox.Show "!! Hello World !!", "My Custom Message box"

This gives you the output as:

Custom Message Box

If you wish to share your experience/insights related to test automation or have discovered some cool new tip related to QTP, contact me using this contact form. [Please select ‘Article’ in the dropdown.]

If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.

Subscribe to get free updates on UFT!

LearnQTP is the most popular site on UFT (formerly QTP).

Enter your first name and email address below to instantly subscribe and download an eBook on Optimizing UFT Scripts. In future, we will make sure you get new tips & tricks on UFT delivered direct to your email box.

Please check your email and confirm free subscription!