Saturday, 27 April 2013


Browser window not getting maximized while using Selenium windowMaximize() method

Unlike QTP, selenium tests can be executed without maximizing the browser window. Unless you click on the browser window, it will not get maximized. So that we can work on other things when the tests gets executed. Though its useful for us, sometimes we want to see whats going on in the web application and we maximize the browser window from taskbar.

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.

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...