Recently, I worked on a requirement in which I was asked to create a custom user-form that lets users choose how they want automated tests to be run. I first thought of creating a windows application using VB.NET, but, after some consideration I decided to use DOTNetFactory instead. This was quite a good choice since I did not need Visual Basic or Visual Studio installed on the PC where I develop my framework. In this series of articles, we will discover how to use the DOTNetFactory to create a user form and how to add different object types to it.
When we use DOTNetFactory, we must first create a COM interface using the CreateInstance method. This interface is passed as an argument (object) which has several methods that we’re going to use in this series.
According to book Scripting QTP, Chapter 14, by Dani Vainstein, the syntax for creating an instance of .NET is:
Set var_CreateInstance = DOTNetFactory.CreateInstance (TypeName [,Assembly] [,args])
TypeName: The full name of the object type, for example, System.Windows.Forms.Form.
Assembly: Optional. If the assembly is preloaded in the registry, you do not need to enter it. Otherwise, you should enter the full path name, for example, System.Windows.Forms. If you want to create an instance of a type in the GAC (Global Assembly Cache), you can enter the short name, for example, Forms.
args: Optional. The required arguments, if any, for the specified TypeName and/or Assembly
Let’s use .CreateInstance to create a simple user form using System.Windows.Forms.Form as the TypeName. A Form object in .NET is part of the System.Windows.Forms namespace. .NET namespaces are beyond the scope of this article, and will be discussed in detail in an upcoming article on VB.NET.
In QTP, we can retrieve the COM interface created by the .CreateInstance method using the ‘Set’ statement:
Set oForm = DOTNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
We have just created a user form using DOTNetFactory!
You might wonder though, if we have really created a user form, where is it? Why doesn’t it display when we execute the above statement? That is because, as I mentioned earlier that the returned COM interface will have several methods. The above form has .Show and .ShowDialog methods which enable the form to be displayed:
Set oForm = DOTNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") oForm.Show
Set oForm = DOTNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") 'Adds a title to the form oForm.Text = "Sample Custom Form" oForm.ShowDialog
Both methods will display the form on your desktop. However, there is a slight difference between the two. When you execute oForm.Show, you will note that the form will be displayed and the test executing will immediately move on to the next line. However, with oForm.ShowDialog, QTP will wait until we perform an event on the form before execution moves on to the next statement in our test. This is the method we’re interested in, because we will be provided QTP with several inputs using these form. In this series, we will be using .ShowDialog extensively.
In the next topic in this series, we will learn how to create a Text Box using DOTNetFactory.
I hope you find this article useful.
References
In this Series
- QTP & DOTNetFactory – Creating a Custom Form {Currently viewing}
- QTP & DOTNetFactory – Adding a Text Box Control
- QTP & DOTNetFactory – Adding a Button Control
- QTP & DOTNetFactory – Adding a CheckBox Control
- QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects
- QTP & DOTNetFactory – Adding a RadioButton
- QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!
If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.
{ 7 comments… read them below or add one }
Excellent work
Thanks! :)
Great Anshoo!!
Thanks Devendra! Its been quite a long time. How have you been?
Hi Anshoo! thnx I have created a form and added a text box and a button (”ok”)..it’s done..but i want to display the message eg:”done” is anyone clicks on the button..so what is the syntax for buton click..
With oForm .Height =500 .Controls.Add oButton End with With oButton If oButton is clicked then ‘i’m searching for this code..plz help as oButton.Click is not working display message End if End withThank you in advance!
Hi Bibek,
Please try this:
Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") Set oButtonOK = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set oButtonCancel = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set DiagReturn = DotNetFactory.CreateInstance("System.Windows.Forms.DialogResult", "System.Windows.Forms") Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point","System.Drawing", x, y) With oPoint .x = 5 .y = 5 End With With oButtonOK .Text = "OK" .Location = oPoint .DialogResult = DiagReturn.Ok End With With oPoint .x = 5 .y = 35 End With With oButtonCancel .Text = "Cancel" .Location = oPoint .DialogResult = DiagReturn.Cancel End With With oForm .Height = 500 .Controls.Add oButtonOK .Controls.Add oButtonCancel .ShowDialog End With If oForm.DialogResult = DiagReturn.Ok Then 'Code to open another form MsgBox "Opening another form.." End If If oForm.DialogResult = DiagReturn.Cancel Then 'Code to do something else MsgBox "Doing something else.." End If Set oForm = Nothing Set oButton = Nothing Set DiagReturn = Nothing Set oPoint = NothingI haven’t tested this code yet. If you experience any issues with it, please let me know and we’ll work together to find a solution that works for you.
Bibek,
I didn’t know you had a blog too! I tried to access the link, but its locked?
{ 5 trackbacks }