Saturday, November 19, 2011

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.

6 comments:

  1. Hello Veera,

    I was looking into this code and thought of giving a try. But I am using C#. Can you please explain what these VK_WINDOWS and VK_M does. So that I can port this code to C#.

    Thanks,
    Naresh

    ReplyDelete
  2. It was very nice article on getting the count.
    I also found an other website which is very helpful with many examples and it is very useful to Selenium beginners
    Selenium Wedriver Tutorials

    ReplyDelete
  3. Thanks for the great information in your blog Selenium Training

    ReplyDelete
  4. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog. Selenium Training in Chennai

    ReplyDelete
  5. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information... Java Training in Chennai | Blue prism Training in Chennai

    ReplyDelete
  6. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.





    mobile phone repair in Novi
    iphone repair in Novi
    cell phone repair in Novi
    phone repair in Novi
    tablet repair in Novi
    ipad repair in Novi
    mobile phone repair Novi
    iphone repair Novi
    cell phone repair Novi
    phone repair Novi

    ReplyDelete