Even we have Selenium windowMaximize() method to maximize the web application. But, if you see, it does maximize!!!, actually it does, but the application (broswer window) will not get the focus. When selenium.windowMaximize() gets executed, the application gets maximized and still it stays in the task bar. Manually we need to click on the browser tab on the taskbar to get the focus of that application.
Normally we use the below code for starting Selenium and maximizing the browser window.
@Before
public void setUp()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
selenium.start();
selenium.windowMaximize();
selenium.windowFocus();
}
@Test
public void testUntitled()
{
selenium.open("/");
.............................
.............................
}
But, most of the times your browser window doesn't get maximized. Some time it gets maximized only if you dont have any other window opened in your taskbar. We have a workaround to resolve this issue. Thats Java Robot class.
We can use Java Robot class to minimize all the windows before opening the URL using Selenium object. We need to activate Window and M keys using robot class. This will minimize all the applications opened. Then we can call selenium.windowMaximize() method which will maximize your browser window. Use the below code.
@Before
public void setUp()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
selenium.start();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_M);
Thread.sleep(1000);
selenium.windowMaximize();
selenium.windowFocus();
selenium.windowMaximize();
selenium.windowFocus();
}
@Test
public void testUntitled()
{
selenium.open("/");
.............................
.............................
}
Hope this code helps to maximize your browser window when you execute your Selenium tests.




