Thursday, December 31, 2009

Browser Control in QTP using DOT Net Factory

Create browser control instance in QTP using DOT Net factory, then you are able to browse any web pages without browsers. Execute the below code,

Set obj = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
Set objBrwsr = DotNetFactory.CreateInstance("System.Windows.Forms.WebBrowser","System.Windows.Forms")

With objBrwsr
End With

With obj .Controls.Add objBrwsrEnd with

Set obj =Nothing
Set objBrwsr=Nothing

You will get DOT Net Dialog with browser control as same as below,

Thanks and regards,

Asiq Ahamed

Accessing HTML Properties using QTP


Getting URL from HTML form.


If you want to access URL from a form's action, use the below code.
Syntax: Browser().Page().Object.getElementsByTagName("form")(IndexOfTheForm).action
Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("form")(0).action

Throughout this article I have used Google for all my examples.

How to find hyperlink's color of your webpage


Here is the code to find link's color of your webpage.

Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.alinkColor

How to find a textbox's auto-complete property is enabled or not


Use the below code to find whether the textbox's auto-complete property is enabled or not.

Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByName("q")(0).autocomplete

Getting default checked property for radio button and checkbox


If you want to know radio button's or checkbox's default checked property in QTP, then use the simple code which I use frequently.

Msgbox Browser("name:=Google").Page("title:=Google").WebRadioGroup("name:=meta").Object.defaultChecked


Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementByID("cty").defaultChecked

Finding host or hostname of the location or URL.


To find host and port number.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).Host

To find host name.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).HostName

Find destination URL


The below code to find a destination URL or an anchor point.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).Href

Part -2 will be published soon

Please refer the below link, if you want to know more about DOM.

Masking Password Using DotNet Factory

If you want to mask your password instead of placing in datatable or encrypting it, then use the below code.

Set oForm=DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
Set oTextBox=DotNetFactory.CreateInstance("System.Windows.Forms.TextBox","System.Windows.Forms")Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)

With oPoint
.x = 120
.y = 20
End With

With oButton
.Location = oPoint
.Width = 100
.Text = "OK"
End with

With oPoint

.x = 0
.y = 20
End With

With oTextBox

.Location = oPoint
End with

'Add Controls

With oForm
.CancelButton = oButton
.Controls.Add oButton
.Controls.Add oTextBox
End with

Msgbox oTextBox.Text

Set oForm=Nothing
Set oTextBox=Nothing
Set oButton=Nothing
Set oPoint=Nothing
Run the above code you will get a DOT Net dialog like below.

Enter your password and click on OK button. The entered password will be displayed in a message box.
For more information about DOT Net controls, please refer the below link.