site
stats
By Anish Pillai Anish Pillai Posted under Not So Common Stuff | QTP Concepts

Different ways to Maximize and Minimize a Browser using QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Many a times you would have noticed that if you open a new browser window using QTP (or otherwise also) the browser is not in maximized mode by default. And in certain cases, you would really need the browser to be in maximized state (like for better readability). In this article, we’ll see how you can maximize and minimize your browser windows using QTP. Unfortunately, there is no direct method to achieve this task as the Browser object does not support Maximize/Minimize actions. The only way to go about this task is to follow some workaround to achieve the desired results. This article is about these indirect methods.


Using Browser Handle with Window Object. Even though Browser object doesn’t provide any methods to maximize/minimize browsers, you have the maximize/minimize functions available with standard Windows object. Hence, you can retrieve the Handle from your Browser object & use the same Handle with the Window object to maximize/minimize your browser. Let’s see few examples for the same –

Sample Script 1: Maximizing a Browser This example maximizes a browser if it is already not maximized and is maximizable.

Dim hwnd, isMaximized, isMaximizable

'Find the handle for the Browser window
hwnd = Browser("CreationTime:=0").Object.HWND

'Check if the Browser is already maximized or not
If Window("hwnd:=" & hwnd).GetROProperty("maximized") = True Then
  isMaximized = True
Else
  isMaximized = False
End If

'Check if the Browser is maximizable or not
If Window("hwnd:=" & hwnd).GetROProperty("maximizable") = True Then
  isMaximizable = True
Else
  isMaximizable = False
End If

'Maximize the browser window if it is not already maximized and is maximizable
If isMaximized = False and isMaximizable = True Then
  Window("hwnd:=" & hwnd).Maximize
End If

Please note that this example fetches the value of hwnd using
hwnd = Browser(“CreationTime:=0”).Object.HWND
rather than using
hwnd = Browser(“CreationTime:=0”).GetROProperty(“hwnd”).
This is because you would find 2 hwnd values for the browser using Object Spy – One available in Identification Properties & one in Native Properties, and both the hwnd properties would have different values. And we should be using the Native HWND Property. Refer the below image for hwnd values.

Hwnd values from Native & Identification Properties

Hwnd values from Native & Identification Properties


Sample Script 2: Minimizing a Browser This example minimizes a browser if it already not minimized and is minimizable.

Dim hwnd, isMinimized, isMinimizable

'Find the handle for the Browser window
hwnd = Browser("CreationTime:=0").Object.HWND

'Check if the Browser is already minimized or not
If Window("hwnd:=" & hwnd).GetROProperty("minimized") = True Then
  isMinimized = True
Else
  isMinimized = False
End If

'Check if the Browser is minimizable or not
If Window("hwnd:=" & hwnd).GetROProperty("minimizable") = True Then
  isMinimizable = True
Else
  isMinimizable = False
End If

'Minimize the browser window if it is not already minimized and is minimizable
If isMinimized = False and isMinimizable = True Then
  Window("hwnd:=" & hwnd).Minimize
End If


Sample Script 3: Use HWND with Descriptive Programming for Maximizing/Minimizing a Browser In this example, the browser properties are not stored in Object Repository, but are taken using Descriptive Programming.

Dim oIE

'Create an object of Internet Explorer type
Set oIE= CreateObject("InternetExplorer.Application")
oIE.visible = True
oIE.navigate "http://www.automationrepository.com"

'Maximize the Browser Window
Window("hwnd:=" & oIE.HWND).Maximize

'Minimize the Browser Window
Wait(2)
Window("hwnd:=" & oIE.HWND).Minimize



Maximizing/Minimizing a Browser while Opening. Here you can make sure that whenever you open a browser using SystemUtil.Run method, it is always opened in Maximized/Minimized mode. Let’s see how this can be done.

Sample Script 4: Use of SystemUtil.Run In this example, the browser will be opened in maximized/minimized mode.

Dim mode_Maximized, mode_Minimized
mode_Maximized = 3 'Open in maximized mode
mode_Minimized = 2 'Open in minimized mode

'Open browser in maximized and minimized mode
SystemUtil.Run "iexplore.exe", "http://www.automationrepository.com", , ,mode_Maximized
SystemUtil.Run "iexplore.exe", "http://www.automationrepository.com", , ,mode_Minimized


Sample Script 5: Use of WScript.Shell RunThis example uses WScript.Shell to open a new browser in maximized/minimized mode.

Dim WshShell, mode_Maximized, mode_Minimized
mode_Maximized = 3 'Open in maximized mode
mode_Minimized = 2 'Open in minimized mode

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "iexplore.exe http://www.automationrepository.com", mode_Maximized, False
WshShell.Run "iexplore.exe http://www.automationrepository.com", mode_Minimized, False
Set WshShell = Nothing


This was all about this article where you saw various methods to minimize/maximize a browser using QTP. Do you know of any other method that has not been mentioned here? Share your thoughts using the Comments section. Happy Reading.. :->

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Arpita

    Great tutorial 🙂

  • Leonid

    Thanks!

  • Mathi

    Excellent

  • manish

    Hello Guys….

    Please find the code below to maximize the browser, it will work on any IE browser (IE 6,7,8,9….)

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

    Thanks
    Manish Soni

    • Guest

      if the browser is in minimized state,then this code works??

      • Anish10110

        Yes.. It would work..

  • Manish

    Why the browser will be in minimized state because when the script is running it will not minimize the browser. If it minimize it then first maximize the browser then user above code. This sode is working fine at my end in different project.

  • PaweÅ‚ Wojtal

    And how could I maximize browser when it's already running and the 1st method is not working?

  • Advaitha

    Hi, While maximizing window, I am getting the following General Run Error

    Line (4): "hwnd = Browser("CreationTime:=0").Object.HWND".

  • Torsten

    Hi Anish,

    what do i have to read to understand this:

    hwnd = Browser("CreationTime:=0").Object.HWND

    Especially the part with "Object.HWND"

    I'm new to VBScript and .NET. I learned OOP through Java…
    Documenation from Microsoft isn't helpful for me, more confusing… Maybe i took a look at the wrong places… ?

    Greetings
    Torsten

  • IK TEST

    Hi All,

    after a little playing with information I created the next script snipped:

    hwnd = Browser("CreationTime:=0"").GetROProperty("top_level_hwnd")

    'Check if the Browser is already maximized or not
    isMaximized = Window("hwnd:=" & hwnd).GetROProperty("maximized")

    'Check if the Browser is maximizable or not
    isMaximizable = Window("hwnd:=" & hwnd).GetROProperty("maximizable")

    'Maximize the browser window if it is not already maximized and is maximizable
    If isMaximized = False and isMaximizable = True Then
    Window("hwnd:=" & hwnd).Maximize
    End If

    Greetings
    An testautomator.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×