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.
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.
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.