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 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", "System.Windows.Forms") MyMsgBox.Show "!! Hello World !!", "My Custom Message box"
This gives you the output as:
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.]
In Vista the process is similar and your goal is “Service Pack 1”.
Under the search results for that website you’ll see a cache of the page you’re trying to access.
Your choice depends on the level of encryption needed for yourself (or to bypass) and your budget.
It’s really nice article sir.
whats the actual use of this technology.
can u evaluate more on dotnet factory ……using tree hierarchy……
Thanks for sharing information about .NetFactory utility object. Now, I got a basic outline about this utility object.
I am stuck with the automation of .Net + infragistic + webform application. It is a standalone application. The scenario which i am trying automate is in hierarchy –
‘SwfWindow().SwfObject().Window().WinObject()’
‘SwfWindow().SwfObject().Window().Page().Frame().WinObject()’
Does you have any idea for automating the above hierarchy.
Please let me know as soon as possible.
Hi Everyone!
I’m a begginer tester, and I’m a bit lost with the scripting yet, I need to go a bit further using DotNetFactory, but I don’t know if it is possible. I would like to get the object loaded in memory instead create a new instance. I’m trying but it just gets empty objects, so I suposse tha is a new instance of it.
My code:
assemblyVec = “C:\Proyectos\Exes\Release\Vec.dll”
assemblyFramework = “C:\Proyectos\Exes\Release\Framework.dll”
assemblyClip = “C:\Proyectos\Exes\Release\Clip.exe”
Set transformacion = DotNetFactory.CreateInstance(“Tool.Vec.Transformacion2T”, assemblyVec, 10, 10)
Set serviciosProyecto = DotNetFactory.CreateInstance(“Tool.Clip.Servicios.Impl.ServiciosProyecto”, assemblyFramework)
Set serviciosProyectoMemory = DotNetFactory.CreateInstance(“Tool.Clip.Servicios.Impl.ServiciosProyecto”)
the vble transformación works because it creates the object it should, but the Vble ServiciosProyecto just have empty properties, I tried to not add the addembly to manage the loaded memory object, but it didn’t work…
Any idea???
Thanks in advance!!!
Hi Nick, you are partially true, there can be lots of benifits using this in terms of ease of use or creating functions for various operations, see part 6, 7 and 8. however it has some added advantages which are like creating forms in QTP, which does not help much in testing applications but there are lots of testers who like them to use in their tests. it does not at all related to .net add in or object identification.
Hi, I think I must be missing the point of the DotNetFactory. I don’t understand how it can help with testing an application. As far as I can make out it only provides functionality to create user defined forms. When I first saw ththese articles I thought that it would allow me to access the properties/methods for objects that QTP recognises as being “winObjects” or the like but after reading them it just seems to provide the ability to create forms for user interactivity. Is this true or what am I missing?
This is really nice, saket.
Sorry Correction to previous comments
It tells me before I install add in I need to install Quick Test Professional so not sure what does that mean.
Please help me with QTP download. I am new with QTP and trying to download software from HP site whenever I download it does 100% download but I can’t install any version ex 8.0 , 9.0, 9.2, or 10. It tells me before I download add in I need to download Quick Test Professional so not sure what does that mean.
Can anyone help me with this.
Thanks,
Meghna
Nice Artical Saket.
hi,
i never used QTP.
how can i start learning QTP?
is it possible to learn it without attending any training classes??
plz help
It is really good script Saket…..
Hi,
I want to be a software tester and I have to start from scratch as I am not from an IT background (though I have underwent training in Winrunner,QTP and Loadrunner from a private institute).Can u please guide me how and where to start from as I am very much interested to secure a job in IT field.
Chandrakanth,
Creating .Net controls is an extra benifit which we get from the utility. Actullay there is no point in creating controls in an automation process, but i found some cases where we need to have these controls, I have tried to explain this in the second part of this series.
Saket,
The DotNetFactory was introduced to make QTP learn the components built in .Net technology and automate them by accessing the properties and methods directly instead of depending on mouse movements or positional programming. There are also version conflicts between QTP and Visual Studio .NET. As you understand, much of the automation time is spent on making the tool learn the application in automation where does creating .NET controls using QTP DotNetFactory fit in ? Can you please enlighten me …
Nice article Saket.
It was really nice Saket, i am looking forward to see advanced usage of DotNetFactory.
Also would be glad if you can give some details on how to use DLLs of a client server application, while automating applications made using third party components. May be something like .. by creating user defined wrapper vbs functions usable by QTP as any of QTP Test Object methods.
this article is very good
Hi Pavan, In the later parts of this series, you will definitely get some more example which can give you a better idea on that.
hi Saket, I am facing general run error while executing the line set objdatetime=DotnetFactory(“System.DateTime”). Can you please let me know what is the issue and is therea ny prerequisite needs to be installed to use the Dotnetfactory objects.
Hi,
Ankur, this is very good way to learn more on QTP and dot net framework.please help me .
Thanks & Regards,
Dev Kumar
It is great kickoff…
I am little confused about the scope of these COM objects usage in real time..
Is it possible for you to give some real time examples instead of the normal date/message box examples, because we can get these date/message box without using the COM object?
This is really good news for QTP practitioners. Along with DP , I can explore with this DOT NET FACTORY.
Thanks Bro!!!!!!!!