Monday, September 9, 2013

Dot Net Factory - Basics

Dot Net Factory is a utility object that enables QTP scripting to directly access methods and properties of Dot Net Object

Because of this method, QTP stands at no limit in its capability.  Using Dot Net Factory, QTP can access the Dot Net’s static methods and properties of a class that does not have instance constructor and also user defined custom dot net classes.

Syntax of Dot Net Factory


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


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

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)


Let us go with examples to get a clear idea,

Example 1:


Working with Dot net Date Parser


Dim dSysDate , objDate
Set dSysDate = Dotnetfactory.CreateInstance(“System.DateTime”) ' Creating an object for Dot Net Component System.DateTime
Set objDate = dSysDate.Parse(“Mon, 9 Sep 2013″)

strDate = objDate.Day & “/” & objDate.Month & “/” & objDate.Year
msgbox strDate
Set dSysDate = Nothing
Set objDate = Nothing

The output will be “09/09/2013”.

Example 2: 

Simple user defined forms 

The below example deals with creating a simple Dot net form with a button, text box and label using dot net component “System.Windows.Forms”

‘Creates a Form
Set objFrm = DotNetFactory.CreateInstance(“System.Windows.Forms.Form”, “System.Windows.Forms”)
‘Creates a Button
Set objBtn = DotNetFactory.CreateInstance(“System.Windows.Forms.Button”, “System.Windows.Forms”)
‘Created a Text Box
Set objEdt = DotNetFactory.CreateInstance(“System.Windows.Forms.TextBox”, “System.Windows.Forms”)
intx=60
inty=30

‘Creates an object to system drawing point to draw a form and to get the locations(X, Y) for the controls
Set objpnt = DotNetFactory.CreateInstance(“System.Drawing.Point”,”System.Drawing”,intx,inty)

‘Creates a label
Set objlbl= DotNetFactory.CreateInstance(“System.Windows.Forms.Label”,”System.Windows.Forms”) 

‘Configuring the label
objlbl.Text=”Enter Password”
objlbl.Location= objpnt

‘Adding the label in Form
objFrm.Controls.Add(objlbl)
objpnt.Y=CInt(objlbl.Height)+40
objEdt.Location= objpnt

objEdt.UseSystemPasswordChar=true ’To set the password character From system
objFrm.Controls.Add(objEdt) ‘Adding the Text box in Form
objBtn.Text=”Close”

objpnt.Y=Cint(objpnt.Y)+CInt(objEdt.Height)+20
objBtn.Location=p1

objFrm.CancelButton= objBtn ‘Adding a button as cancel button in form
objFrm.Controls.Add(objBtn)
objFrm.Text=”Dot Net Factory Dialog Box”
objFrm.StartPosition=CenterScreen
objForm.ShowDialog ‘Showing the designed form


The above mentioned method is suitable for simple forms and may look little complex when we are creating a complex form or form with more controls.

Most of the time we prefer IDE based design for designing a form rather then creating them using code, so let’s look into the IDE based form design in my next article on Dot Net Factory.



1 comment:

  1. Please tell me how to check if a checkbox within the form is true or false using loop (consider there is 10 check boxes and I wan to check which all are cheked by the user)

    ReplyDelete