Relevant Codes

A Test Development Resource for HP QuickTest Professional.

QTP Browser Methods: .Activate .Maximize .Minimize

by Anshoo Arora on March 8, 2010

This article demonstrates a quick tip to activate, minimize or maximize browsers. Unlike a Standard Windows Window object, Browser does not support the Activate, Minimize, Maximize methods. Therefore, we can create our custom function and tie it with the Browser object using RegisterUserFunc.

The only trick is to retrieve the Browser Handle and substitute the handle in the description of a Window object, and use the Window’s Activate method instead.

Activate Browsers:

Function BrowserActivate(Object)
	Dim hWnd
 
	hWnd = Object.GetROProperty("hwnd")
 
	On Error Resume Next
		Window("hwnd:=" & hWnd).Activate
 
		If Err.Number <> 0 Then
			Window("hwnd:=" & Browser("hwnd:=" & hWnd).Object.hWnd).Activate
			Err.Clear
		End If
	On Error Goto 0
End Function
 
RegisterUserFunc "Browser", "Activate", "BrowserActivate"

After registering the BrowserActivate function with the Browser object as Activate we can use it just like we would use it for a Window object:

Browser("title:=Relevant Codes.*").Activate

BrowserActivate can be extended to maximize and minimize a browser window as well. The only extra statement to be included in the function would be the maximize and minimize methods of the window object.

Minimize Browsers:

Function BrowserMinimize(Object)
	Dim hWnd
 
	hWnd = Object.GetROProperty("hwnd")
 
	On Error Resume Next
		Window("hwnd:=" & hWnd).Activate
 
		If Err.Number <> 0 Then
			hWnd = Browser("hwnd:=" & hWnd).Object.hWnd
			Window("hwnd:=" & hWnd).Activate
			Err.Clear
		End If
 
		Window("hwnd:=" & hWnd).Minimize
	On Error Goto 0
End Function
 
RegisterUserFunc "Browser", "Minimize", "BrowserMinimize"

Maximize Browsers:

Function BrowserMaximize(Object)
	Dim hWnd
 
	hWnd = Object.GetROProperty("hwnd")
 
	On Error Resume Next
		Window("hwnd:=" & hWnd).Activate
 
		If Err.Number <> 0 Then
			hWnd = Browser("hwnd:=" & hWnd).Object.hWnd
			Window("hwnd:=" & hWnd).Activate
			Err.Clear
		End If
 
		Window("hwnd:=" & hWnd).Maximize
	On Error Goto 0
End Function
 
RegisterUserFunc "Browser", "Maximize", "BrowserMaximize"

If you would like to use the above 3 methods through a single function or class, they can be coupled together through Execute statements or through If-Then or Switch-Case blocks. Happy reading!

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.

{ 20 comments… read them below or add one }

1 qualitypoint March 8, 2010 at 11:51 am
2 Anshoo Arora March 9, 2010 at 4:20 pm

:)

Reply

3 sonalee May 3, 2010 at 6:39 am

Hey anshoo

r u really doing a grt job
keep it on ….I am a beginner in QTP so some of ur codes are bouncing over my head :)
plzz can u suggest me some good sites where I can get every tit bit idea abt QTP and after tht i will go thru ur codes I will get tht completely

Reply

4 Anshoo Arora May 5, 2010 at 2:35 am

I have included a few websites in the Links section of Relevant Codes. They are all excellent resources which will certainly be helpful to you :)

Reply

5 Harish May 6, 2010 at 1:14 am

hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd

Here, handle of the run-time object’s window ‘hWnd’ is used to identify Browser object.
But, I did not get the idea of getting the same value.

Could you please explain me about this?

Reply

6 Anshoo Arora May 6, 2010 at 1:41 pm

This is a workaround for IE7+ versions because of tabbed browsing. If you have IE7 or greater, use your Object Spy to spy on the Browser object and you will find 2 different hWnd values – Test and Native. Thus, the Test time hWnd is used to retrieve the Native hWnd.

Reply

7 Harish May 6, 2010 at 10:44 pm

Thank you…:-)

Reply

8 Deep May 9, 2010 at 10:59 am

Hi Good Effort , Thank you

Hey , If any one have idea please tell me “Why” QTP Does not support
tab browsing ?

Reply

9 Anshoo Arora May 10, 2010 at 10:22 am

Deep,

What version of QTP are you using?

Reply

10 Anonymous May 10, 2010 at 12:38 pm

Yes Anshoo

I am using QTP 9.0
Does the version matters for Tab

Is there any version which supports Tab in browser.

Reply

11 Deep May 10, 2010 at 12:39 pm

Yes Anshoo

I am using QTP 9.0
Does the version matters for Tab

Is there any version which supports Tab in browser.

Reply

12 Anshoo Arora May 10, 2010 at 12:42 pm

Deep,

I think tabbed browsing was supported after version 9.5..

Reply

13 amar May 14, 2010 at 5:52 am

You are doing gr8 job , Thank you!!!!

Reply

14 sunny June 8, 2010 at 10:13 am

how do u capture the browser object when it happens to be the parent object….i used the followin code and it errors out…

Set oBrowser = Description.Create()
oBrowser(“micclass”).value = “Browser”
oBrowser(“name”).Value = “Edit Study.*”
oBrowser(“title”).Value = “Edit Study.*”

hWnd = oBrowser.GetROProperty(“hwnd”)…..it errors out over here…

Reply

15 Anshoo Arora June 8, 2010 at 10:43 am

Hi Sunny,

You cannot directly use the description object like that. It should be this instead:

Set oBrowser = Description.Create()
oBrowser("micclass").value = "Browser"
oBrowser("name").Value = "Edit Study.*"
oBrowser("title").Value = "Edit Study.*"

hWnd = Browser(oBrowser).GetROProperty("hwnd")

Reply

16 edwina July 7, 2010 at 4:45 am

how u will open a page using browser navigation

Reply

17 edwina July 7, 2010 at 4:47 am

what is hwnd in programming

Reply

18 Sivaram S August 11, 2010 at 2:28 am

Hi edwina,

A hWnd is the abbreviation of “Window Handle” which is an unique identifier. The browser or windows are assigning unique value to this property. So, we can use this property for the browser identification.

Thanks,
Sivaram S

Reply

19 Anonymous August 16, 2010 at 5:54 am

hi

in the above conversations , i have seen a code
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd

could you highlite some information on { .object.hwnd} in the above line of code. i mean how exatcly this .object works

Thanks in advance
Vasudev

Reply

20 Anshoo Arora August 22, 2010 at 4:38 pm

When you see .Object being used, it means, the code is working with the native properties instead of TO properties. In the object spy, there are radio buttons for Test Object properties as well as Native properties. The use of .Object points to the latter.

Reply

Leave a Comment

Previous post:

Next post: