As per the QTP:
The DotNetFactory object in QTP enables you to create an instance of a .NET object, and access its methods and properties.
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.
Also the .Net Add-in is not required for this to work. I personally feel that the utility has been introduced to resolve the problem that occured when referencing a .Net Assembly or C/C++ DLL and making a call to its functions.
The piece of code would be as in the following:
Set myMessageBox = DotNetFactory.CreateInstance (TypeName [,Assembly] [,args])
Set myMessageBox =
DotNetFactory.CreateInstance("System.Windows.Forms.Form",
"System.Windows.Forms")
myMessageBox.Show()
Create the custom UI forms part of my automation test framework so the tester can run opted test scripts as per the user's choice using the .Net factory utility:
The entire code of the Test Method would be as in the following:
'Form
UIForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
'Logo Image
imgLogo = DotNetFactory.CreateInstance("System.Windows.Forms.PictureBox", "System.Windows.Forms")
imgLogoSize = DotNetFactory.CreateInstance("System.Windows.Forms.PictureBoxSizeMode", "System.Windows.Forms")
'Form Background Image
frmImage = DotNetFactory.CreateInstance("System.Drawing.Image", "System.Windows.Forms")
'Labels
lblHosts = DotNetFactory.CreateInstance("System.Windows.Forms.Label", "System.Windows.Forms")
lblRun = DotNetFactory.CreateInstance("System.Windows.Forms.Label", "System.Windows.Forms")
lblCancel = DotNetFactory.CreateInstance("System.Windows.Forms.Label", "System.Windows.Forms")
lblLog = DotNetFactory.CreateInstance("System.Windows.Forms.Label", "System.Windows.Forms")
'Buttons
btnRun = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
btnCancel = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
dResult = DotNetFactory.CreateInstance("System.Windows.Forms.DialogResult", "System.Windows.Forms")
'Radio Buttons
rdDTAOption = DotNetFactory.CreateInstance("System.Windows.Forms.RadioButton", "System.Windows.Forms")
rdCONFIGOption = DotNetFactory.CreateInstance("System.Windows.Forms.RadioButton", "System.Windows.Forms")
'Radio Buttons POS
rdDTAOptionPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
rdCONFIGOptionPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
'Check Boxes
chkWindows = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkAix = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkHpux11 = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkHpuxi = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkRedhat = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkSun480 = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkTru64 = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
chkZlnx = DotNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
' Progress Bar
frmProgressBar = DotNetFactory.CreateInstance("System.Windows.Forms.ProgressBar", "System.Windows.Forms")
' Text Box
txtLog = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")
'Labels POS
lblHostsPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
lblRunPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
lblCancelPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
lblLogPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
'Buttons POS
btnRunPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
btnCancelPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
frmImagePOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
'Check Boxes POS
chkWindowsPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkAixPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkHpux11Pos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkHpuxiPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkRedhatPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkSun480Pos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkTru64Pos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
chkZlnxPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
' Progress Bar POS
frmProgressBarlPOS = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
' Text Box POS
txtLogPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
Var = DotNetFactory.CreateInstance("System.Environment")
'Logo Image POS
imgLogoPos = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing")
'Logo Image Properties
With imgLogoPos
.X = 1
.Y = -1
End With
With imgLogo
.ImageLocation = "~ \Images\Icon.jpg"
.Location = imgLogoPos
.SizeMode = imgLogoSize.Autosize
.Width = 580
.Height = 91
End With
'Radio Buttons Properties
With rdDTAOptionPos
.X = 32
.Y = 96
End With
With rdDTAOption
.Location = rdDTAOptionPos
'.BackColor=System.Drawing.Color.Transparent
.Text = "Path From DTA Batch Test CONFIG File"
.Name = "rdDTAOption"
.Width = 239
.Height = 20
End With
With rdCONFIGOptionPos
.X = 335
.Y = 96
End With
With rdCONFIGOption
.Location = rdCONFIGOptionPos
'.BackColor=System.Drawing.Color.Transparent
.Text = "Path From Regression Suite CONFIG File"
.Name = "rdCONFIGOption"
.Width = 250
.Height = 19
End With
' Label Selected Hosts Properties
With lblHostsPOS
.X = 29
.Y = 126
End With
With lblHosts
.Text = "Select the required Hosts to run the Regression Test :"
.Location = lblHostsPOS
.Width = 325
.Height = 17
End With
'Check Boxes Properties
With chkWindowsPos
.X = 32
.Y = 160
End With
With chkWindows
.Location = chkWindowsPos
'.BackColor=System.Drawing.Color.Transparent
.Text = "WINDOWS"
.Name = "chkWindows"
.Width = 82
.Height = 20
End With
With chkAixPos
.X = 171
.Y = 160
End With
With chkAix
.Location = chkAixPos
.Text = "AIX"
.Name = "chkAix"
.Width = 82
.Height = 20
End With
With chkHpux11Pos
.X = 335
.Y = 160
End With
With chkHpux11
.Location = chkHpux11Pos
.Text = "HPUX11"
.Name = "chkHpux11"
.Width = 82
.Height = 20
End With
With chkHpuxiPos
.X = 470
.Y = 160
End With
With chkHpuxi
.Location = chkHpuxiPos
.Text = "HPUXI"
.Name = "chkHpuxi"
.Width = 82
.Height = 20
End With
With chkRedhatPos
.X = 32
.Y = 196
End With
With chkRedhat
.Location = chkRedhatPos
.Text = "REDHAT"
.Name = "chkRedhat"
.Width = 82
.Height = 20
End With
With chkSun480Pos
.X = 171
.Y = 196
End With
With chkSun480
.Location = chkSun480Pos
.Text = "SUN480"
.Name = "chkSun480"
.Width = 82
.Height = 20
End With
With chkTru64Pos
.X = 335
.Y = 196
End With
With chkTru64
.Location = chkTru64Pos
.Text = "TRU64"
.Name = "chkTru64"
.Width = 82
.Height = 20
End With
With chkZlnxPos
.X = 470
.Y = 196
End With
With chkZlnx
.Location = chkZlnxPos
.Text = "ZLNX"
.Name = "chkZlnx"
.Width = 82
.Height = 20
End With
' Label Log Information Properties
With lblLogPOS
.X = 29
.Y = 243
End With
With lblLog
.Text = "Log Information :"
.Location = lblLogPOS
.Width = 110
.Height = 17
End With
'lblLog.Font.Size=10
'lblLog.Font.Bold = True
' Text Box Log Information Properties
With txtLogPos
.X = 32
.Y = 282
End With
With txtLog
.Location = txtLogPos
.Text = ""
.Name = "txtLog"
.Multiline = True
.Width = 505
.Height = 122
End With
' Label Run Regression Properties
With lblRunPOS
.X = 332
.Y = 243
End With
With lblRun
.Text = "Run Regression"
.Location = lblRunPOS
.Width = 110
.Height = 17
End With
' Button Run Application Properties
With btnRunPOS
.X = 453
.Y = 240
End With
With btnRun
.Text = ""
.Location = btnRunPOS
.Image = frmImage.FromFile("D:\RegressionSuite\Images\play.bmp")
.DialogResult = dResult.OK
.Width = 85
.Height = 25
End With
' Progressive Bar Properties
With frmProgressBarlPOS
.X = 32
.Y = 410
End With
With frmProgressBar
.Location = frmProgressBarlPOS
.Width = 505
.Height = 25
End With
' Label Exit Application Properties
With lblCancelPOS
.X = 332
.Y = 442
End With
With lblCancel
.Text = "Exit Application"
.Location = lblCancelPOS
.Width = 100
.Height = 15
End With
' Button Exit Application Properties
With btnCancelPOS
.X = 453
.Y = 439
End With
With btnCancel
.Text = ""
.Location = btnCancelPOS
.DialogResult = dResult.Cancel
.Image = frmImage.FromFile("D:\RegressionSuite\Images\stop.bmp")
.Width = 85
.Height = 25
End With
With frmImagePOS
.X = 0
.Y = 0
End With
With UIForm
'.Controls.Add frmImage
'.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
'.Opacity = 0.7
'.ClientSize = frmSize.Size(376, 208)
.Controls.Add(imgLogo)
.Controls.Add(rdDTAOption)
.Controls.Add(rdCONFIGOption)
.Controls.Add(frmProgressBar)
.Controls.Add(lblHosts)
.Controls.Add(chkWindows)
.Controls.Add(chkAix)
.Controls.Add(chkHpux11)
.Controls.Add(chkHpuxi)
.Controls.Add(chkRedhat)
.Controls.Add(chkSun480)
.Controls.Add(chkTru64)
.Controls.Add(chkZlnx)
.Controls.Add(lblRun)
.Controls.Add(btnRun)
.Controls.Add(lblLog)
.Controls.Add(txtLog)
.Controls.Add(lblCancel)
.Controls.Add(btnCancel)
.Name = "UIForm"
.Text = "My Custom User Form"
.MaximizeBox = False
.Minimizebox = False
.Width = 592
.Height = 556
.Location.X = 100
.Location.Y = 100
'.ClientSize = frmSize(573, 474);
'.BackgroundImage = frmImage.FromFile("D:\RegressionSuite\Images\BackImage_2.jpg")
'.BackgroundImageLayout="Stretch"
.ShowDialog()
End With
'msgbox MyCheckBox.Checked
'msgbox "User Form Closed"
If UIForm.DialogResult = dResult.OK Then
If chkWindows.Checked = "True" Then
CreateRegressionScripts("NT4")
ElseIf chkAix.Checked = "True" Then
CreateRegressionScripts("AIX")
ElseIf chkHpux11.Checked = "True" Then
CreateRegressionScripts("HPUX11")
ElseIf chkHpuxi.Checked = "True" Then
CreateRegressionScripts("HPUXI")
ElseIf chkRedhat.Checked = "True" Then
CreateRegressionScripts("LNX")
ElseIf chkSun480.Checked = True Then
CreateRegressionScripts("SUN480")
ElseIf chkTru64.Checked = "True" Then
CreateRegressionScripts("OSF")
ElseIf chkZlnx.Checked = "True" Then
CreateRegressionScripts("ZLNX")
End If
Else
MsgBox("Close Button Pressed")
End If