Pages

Monday, 27 February 2012

Different ways to Maximize and Minimize a Browser using QTP


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
Dim oIE
 
'Create an object of Internet Explorer type
Set oIE= CreateObject("InternetExplorer.Application")
oIE.visible = True
 
'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.
1
2
3
4
5
6
7
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.
1
2
3
4
5
6
7
8
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

2 comments:

  1. Good answers in return of this matter with solid arguments and explaining all about that.
    My web blog :: trigeminal neuralgia Tmj

    ReplyDelete
  2. Hi! I'm at work surfing around your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the superb work!

    my page :: diets that work

    ReplyDelete