Monday, October 31, 2011

Maximize browser window using java robot in Selenium RC

Maximize browser window using java robot in Selenium RC, Selenium Notes, Videos, Examples By.www.laxmiroy.blogspot.com/


Have you noticed that selenium RC does not maximize the browser window even after invoking the selenium.windowMaximize( ) ? The window is not stretched-out fully.


The only way to maximize the browser window fully I suppose is to use the java robot to do a keyboard simulation of ALT+SPACE which displays the main window's system menu and then using the down arrow key reach the maximize option and then do an ENTER to invoke this option.

Below is the code that will do this for you.
//maximize the window as much a possible using selenium

  selenium.windowMaximize();

//bring the browser window into focus

  selenium.windowFocus();

//invoke the system menu using robot 

  Robot robot = new Robot();
  robot.keyPress(KeyEvent.VK_ALT);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyRelease(KeyEvent.VK_ALT);
  robot.keyRelease(KeyEvent.VK_SPACE);
  Thread.sleep(1000);

//Press down arrow key to move down the menu

       robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  Thread.sleep(100);

  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  Thread.sleep(100);

  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  Thread.sleep(100);

  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  Thread.sleep(100);

//Press enter to invoke the Maximize menu option  

       robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);

Don't forget to import the following in your test class.


import java.awt.Robot;
import java.awt.event.KeyEvent;

no need to ad anything to environment variable. try this

SeleniumServer seleniumserver=new SeleniumServer();
seleniumserver.boot();
seleniumserver.start();
setUp("http://www.imdb.com/", "*firefox");
selenium.open("/");
selenium.windowMaximize();
selenium.windowFocus();

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
Thread.sleep(1000);

//Press down arrow key to move down the menu

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

//Press enter to invoke the Maximize menu option

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

1 comment: