QTP – Browser’s Activate Maximize Minimize

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 Browser
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!

Subscribe for Relevant Codes newsletter to get awesome updates and articles delivered straight to your inbox.

Leave a Comment

{ 61 comments… add one }

  • Rups July 19, 2013, 1:59 am

    Hi guys,

    For some reason the maximize and minimize code doesn’t work for me. It doesn’t give me any errors but it doesn’t maximise or minimize either. I’m using IE8 and read somewhere that the code for IE8 is different? Please note, I do not want to use the Fullscreen method. I just want to maximize the browser. Please help!

    Reply
  • manish soni May 12, 2013, 11:32 pm

    Set WshShell = CreateObject(“WScript.Shell”)
    WshShell.SendKeys “%{ }”
    wait(3)
    WshShell.SendKeys ” x”
    Set WshShell=Nothing
    wait(2)

    Reply
  • Rubin February 4, 2013, 8:30 am

    Hi,

    Is it possible to use the RegisterUserFunc to add a new method called “Type” which sends input character by character rather than “Set” which sets the entire string at one go?

    Something like the WinEdit object’s Type method.

    Rubin

    Reply
  • Vinay Sagar February 17, 2012, 12:08 pm

    hi Anshoo,
    your solutions are very much helpful to me !

    i have a query… i met with a situation in which there are two buttons say B1 and B2 in a page and clicking on each button they display diffirent links (Something like a dropdown) and each link is identified as a webelement in QTP. Each link upon clicking will open a new window. All the links are dynamic in nature, their object name or innertext or outer text are always changing.Now my question is there are two links under B1 and B2 respectively with the same name as “Style”. The inner text or outer text of both of them are same again as “Style”. Now can you help me out in how to identify the link “Style” under B2 and click it using QTP in descriptive method ??

    Reply
    • Anshoo Arora March 15, 2012, 6:54 am

      Vinay, there are several things you can do to work around this situation. I’m going to break it down into a list:

      1. Using Index, not sure how reliable it is though in your case
      2. Something to consider is working with the developers to add an ‘id’ property to it
      3. You could also use visual relational identifiers if you’re on QTP 11.0
      4. If each button is in a WebTable, use WebTable methods (ChildItem etc.) to get to the correct object

  • Maulik September 24, 2011, 11:19 pm

    thanks mate.
    it worked for me :)

    Reply
  • Maulik September 23, 2011, 3:10 am

    Hi anshoo,

    I am trying to fetch hwnd value of a browser using following code.

    Function fnWEB_objBrowserPage()
    On error resume next
    Set oBrowser = nothing
    Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
    Hwnd = extern.FindWindow(“IEFrame”,vbNullString) ‘ returns value of hwnd
    Set oBrowser = Description.Create
    oBrowser (“hwnd”).value = Hwnd
    ‘Window(“HWND:=”&Hwnd).Activate
    Set oBrowser1 = Browser(oBrowser)
    Set oBrowser = Browser(oBrowser).Page(“micClass:=Page”)
    ‘msgbox oBrowser.getROProperty(“title”)
    On error GoTo 0
    End Function
    ‘End of code

    extern.FindWindow(“IEFrame”,vbNullString) returns ‘native’ value of an open browser
    however when i use this property, QTP is not able to identify the browser using hwnd property since it checks for the identification hwnd property.
    it is working fine with IE6 but not working for IE7 and above.
    using object spy, i have observed that the native HWND property value and identification HWND property value is different
    Is there any solution for this ??

    Regards,
    Maulik

    Reply
    • Anshoo Arora September 24, 2011, 9:40 pm

      Maulik, try this:

      Function fnWEB_objBrowserPage()
      	On error resume next
      		Set oBrowser = nothing
      
      		Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
      		Hwnd = extern.FindWindow("IEFrame",vbNullString) ' returns value of hwnd
      
      		Set oBrowser = Description.Create
      		oBrowser ("hwnd").value = Hwnd
      
      		nativeHWND = Browser("hwnd:=" & Hwnd).Object.hWnd
      		Window("HWND:="& nativeHWND).Activate
      
      		Set oBrowser1 = Browser(oBrowser)
      		Set oBrowser = Browser(oBrowser).Page("micClass:=Page")
      
      		'msgbox oBrowser.getROProperty("title")
      	On error GoTo 0
      End Function
      
  • Ramesh September 1, 2011, 4:15 am

    Hi Anshoo,

    I want to Connect IBM DB2 database. I used the following lines to connect. when i run the below code i get a Error message “[[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    Line (6): “cnMidas.Open”. ”
    Set cn = CreateObject(“ADODB.Connection”)
    connectionstr =”Driver={IBM DB2 ODBC DRIVER};Database=myDatabase;Hostname=myserver Address;Port=1234;Protocol=TCPIP;Uid=myuserID;Pwd=mypassword;”
    cn.connectionstring = connectionstr
    cn.Open
    If cn.state=1 then
    msgbox “Pass”
    Else
    msgbox “Fail”
    End If

    Can you please help me regarding this issue?

    Reply
  • Imtiaz August 12, 2011, 1:28 am

    System and Tools details

    OS = Window XP , QTP Version 10.0 , IE7

    Hi Anshoo,

    I am not able to execute successfully

    I am using this code ,

    Function BrowserActivate(Object)
    Dim hWnd

    hWnd = Browser(“hwnd:=” & hWnd).Object.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”

    Function BrowserMinimize(Object)
    Dim hWnd

    hWnd = Browser(“hwnd:=” & hWnd).Object.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”

    Set obj = Description.create()

    obj(“micclass”).value = “Browser”

    obj(“name”).value = “QTP Browser Methods.*” ‘ : .Activate .Maximize .Minimize — Relevant Codes (by Anshoo Arora)”

    obj(“title”).value = “QTP Browser Methods.*” ‘: .Activate .Maximize .Minimize — Relevant Codes (by Anshoo Arora) – Windows Internet Explorer”

    Call BrowserMinimize(obj)

    I am getting this error

    “Cannot identify the object “[ Browser ]” (of class Browser). Verify that this object’s properties match an object currently displayed in your application.

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

    Reply
    • Anshoo Arora August 16, 2011, 8:47 pm

      Imtiaz, not sure what went wrong there but please try this as an example:

      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"
      
      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"
      
      'code
      SystemUtil.Run "iexplore.exe", "http://google.com", , , 3
      
      With Browser("title:=Google")
      	If .Exist(15) Then
      		.Activate
      		.Minimize
      	End If
      End With
      
  • Anonymous August 3, 2011, 9:11 am

    Hi Anshoo,

    Im Automating putty using QTP. here i need to run some jobs using Unix commands for 2 different environment and compare the 2 resulting records. is this possible with QTP??

    Reply
  • Ramesh August 1, 2011, 8:07 am

    Can any one please tell me what addins are required for Autosys tool. i used Java Addin but only the window and java table is getting identified. please help me

    Reply
  • Srinu July 15, 2011, 7:12 am

    Could u please any one post the script for icicibank login ??

    Reply
  • Anonymous July 15, 2011, 4:33 am

    I want to close all opened windows(not browsers) from QTP. I used the below code to run but my system is getting retarted. Can you please tell me why is this happening and correct it if its wrong.

    SystemUtil.CloseProcessByName(“explorer.exe”)

    Reply
    • Anshoo Arora July 17, 2011, 7:52 pm

      You’re shutting down “explorer.exe” – also known as Windows Explorer. The behavior you see is the desired or correct behavior. You need to give the name of the Process that you would like to close. Other technique is using CloseProcessByWndTitle – Close Process by Window Title.

  • Ramesh July 13, 2011, 6:54 am

    Hi Anshoo,

    i want a QTP script which can open a excel sheet and delete a particulare rows. Say if the Cell value is 0 or some values like “*_ter”. Can you please help me.

    Reply
  • Daniel June 13, 2011, 9:04 am

    Hi,

    I have a Browser object that I have been using QTP to record against

    Browser(“Certificate Error: Navigation_2″).Page(“Page_3″).Frame(“Frame_2″)

    I need to do use the following function “.Type “CityName” + micDwn” – however, the Type function is not available on Browser objects, only on Windows objects.

    Is there a way to convert my browser object to a windows object so I can use Type ?

    Thanks,

    Daniel

    Reply
    • Anshoo Arora June 24, 2011, 11:05 am

      Daniel: The Type method cannot be converted to be used in the Web Environment. You can use SendKeys as a replacement:

      CreateObject("WScript.Shell").SendKeys "value"
      
  • Anonymous June 9, 2011, 8:19 am

    Hi anshoo,

    im a regular reader of this site and thought of asking the below issue
    while loading the script in to QTP, QTP gets closed Automatically. can you please tell me y this is happening.
    im using QTP version 9.2

    Reply
    • Anshoo Arora June 24, 2011, 10:53 am

      Can you make sure this is not because of a script issue? Are you performing a custom (yet illegal) DOM operation? If not either of these, can you reinstall QTP and see if that helps?

  • Uday June 6, 2011, 10:25 am

    This is really a good information and I have used this to take screen shot of the application.

    If I didnt activate the browser the TSL script screen shot is getting displayed

    Reply
  • Israel Valencia February 23, 2011, 7:38 pm

    Loved your solution for these three unsupported methods by the Browser objects!!

    While playing with the code (QTP10, IE8, Win7) I figured out some things and added some extra validations for those who like to report everything like me :)

    msgbox BrowserMaximize(oBR)

    Function BrowserMaximize(oBrowser)
    Dim hWnd, oWindow, bMaximizable, bMaximized

    If oBrowser.Exist(0) = False then Exit Function
    hWnd = oBrowser.GetROProperty(“hwnd”) ‘the returned value corresponds to the “page” hwnd
    Set oWindow = Window(“hwnd:=” & hWnd) ‘You can verify this by:
    oWindow.Highlight ‘highlighting the object window that has this hwnd property, it will highlight only the page section
    ‘and if you perform action oWindow.Activate at this time you will always get an error since _
    ‘this object cannot be activated the same way a window is activated :P
    hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd ‘now let’s get the window hWnd with the object’s native properties
    On Error Resume Next ‘this should not be needed now
    Set oWindow = Window(“hwnd:=” & hWnd) ‘set the oWindow to the window with the new hWnd
    oWindow.Highlight ‘when you highlight this object you can see it highlights the whole window :)
    bMaximizable = oWindow.GetROProperty(“maximizable”) ‘you can read this property to check if the window is maximizable
    bMaximized = oWindow.GetROProperty(“maximized”) ‘and you can read this one to check if the window is already maximized :)
    If bMaximizable Then
    If bMaximized = False Then
    If Err.Number 0 Then
    msgbox “there was an error” ‘this message box should not appear
    hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd ‘so this line is not needed anymore
    Window(“hwnd:=” & hWnd).Activate ‘not needed
    Err.Clear ‘ :)
    End If

    oWindow.Maximize ‘what you wanted to do, this line should not return any error :)

    If err.number 0 Then ‘in case, only in case there was an unexpected error…
    msgbox “there was an unexpected error: ” & err.description
    BrowserMaximize = False
    End If
    On Error Goto 0
    Else
    Msgbox “browser window already maximized :)”
    BrowserMaximize = True
    End If
    Else
    Msgbox “Browser window cannot be maximized. Please verify” ‘of course you may want to remove all msgbox and highlight lines away :)
    BrowserMaximize = False
    End If
    End Function

    Regards,
    Israel

    Reply
    • Anshoo Arora February 28, 2011, 7:32 am

      Neat solution Israel and I really like the use of the two properties ‘maximizable’ and ‘maximized’ :)

  • imtiaz December 27, 2010, 6:41 am

    Hi Ashoo,

    I am getting a error message as “The operation cannot be performed” and getting failed and warning as “Object’s physical description:hwnd = 529660″

    I am using IE7 .

    Could you please help to solve this problem.

    Reply
    • Anshoo Arora January 2, 2011, 7:04 pm

      Imtiaz,

      I have not been able to reproduce this issue on IE7. What procedure produces this error? Can you please share the code you’ve used?

  • Prikay December 27, 2010, 4:22 am

    vry useful info.

    Reply
  • ravi December 22, 2010, 2:00 am

    Hi anshoo many many thanks for ur reply.

    Here RegisterUserFunc is it the built in function of qtp ?

    Reply
    • Anshoo Arora December 23, 2010, 3:29 pm

      Ravi,

      Yes, RegisterUserFunc is a QTP specific function.

    • ravi December 27, 2010, 9:15 am

      thanks anshoo. i will get back to u with more doubts.

    • ravi January 16, 2011, 8:26 am

      Hi ANSHOO,

      i would like to know abt the RegisterUserFunc parameters.

      i have seen few examples they are writing the [ RegisterUserFunc " " , " ", " " ] statement with 3 parameters but nt mentioning the syntax more clearly . i would like to know with example can u tell me in detail.

    • Anshoo Arora January 22, 2011, 7:19 am

      Ravi: RegisterUserFunc syntax:

      RegisterUserFunc TOClass, MethodName, FunctionName, SetAsDefault 
      

      From help:

      TOClass: The test object class for which you want to register the method.
      MethodName: The method you want to register. The method name cannot contain spaces.
      FunctionName: The name of your user-defined function. The function can be located in your action or in any library file associated with your test or component.
      SetAsDefault: Optional. Indicates whether the registered function is used as the default operation for the test object.

    • ravi January 23, 2011, 12:19 am

      Hi Anshoo ,

      thank u soo much .. actually am in to development background but these very much willing and eager to learn qtp. so i found relavent codes site which gives me more technical stuff than other sites.

      once again i thank u vey much.

    • Anshoo Arora January 23, 2011, 4:22 am

      You should also visit AdvancedQTP. Lots of technical information there.

    • ravi January 23, 2011, 6:46 am

      Hi anshoo,
      yes i visited advanced qtp.. and i like the qtp videos site http://motevich.blogspot.com/

  • ravi December 14, 2010, 2:07 am

    Hi Anshoo ,
    i just want to know about few things that u have used in the coding… what is
    On Error Goto 0 : ? and wat exactly it will do and wat is the use of it.
    RegisterUserFunc : is it the function and u r passing the parametrs to it ? pls explain

    Thanks in advance
    Ravi

    Reply
    • Anshoo Arora December 19, 2010, 7:44 pm

      1. On Error Goto 0 is the exit statement of On Error Resume Next. Its like For..Next, or IF-Then-End If.

      2. RegisterUserFunc from QTP help:

      Enables you to add new methods to test object classes or change the behavior of an existing test object method during a run session.

  • sumanta das December 9, 2010, 4:47 pm

    Thanks Anshoo. It was really helpful.

    Reply
  • Imtiaz November 5, 2010, 10:23 am

    I tryies the above code but it is not working , there is no error while executing the code but Browser is not maximizing nor minimizing.

    Please help me .. to mini and maxi the Browser , i am using IE 7 version

    Reply
  • Anonymous August 16, 2010, 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
    • Anshoo Arora August 22, 2010, 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.

    • Prasad December 3, 2010, 1:32 pm

      Now really i got a better idea abt Test object properties and native properties

      Thanks

  • edwina July 7, 2010, 4:47 am

    what is hwnd in programming

    Reply
    • Sivaram S August 11, 2010, 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

  • edwina July 7, 2010, 4:45 am

    how u will open a page using browser navigation

    Reply
  • sunny June 8, 2010, 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
    • Anshoo Arora June 8, 2010, 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")
      
  • amar May 14, 2010, 5:52 am

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

    Reply
  • Deep May 9, 2010, 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
    • Anshoo Arora May 10, 2010, 10:22 am

      Deep,

      What version of QTP are you using?

    • Anonymous May 10, 2010, 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.

    • Deep May 10, 2010, 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.

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

      Deep,

      I think tabbed browsing was supported after version 9.5..

  • Harish May 6, 2010, 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
    • Anshoo Arora May 6, 2010, 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.

    • Harish May 6, 2010, 10:44 pm

      Thank you…:-)

  • sonalee May 3, 2010, 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
    • Anshoo Arora May 5, 2010, 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 :)

  • qualitypoint March 8, 2010, 11:51 am

    Thanks for the useful info.
    http://qualitypointtech.net/ebook/index.php

    Reply