Monday, October 26, 2009

Maximize browser window using java robot in Selenium RC

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;

20 Comments:

webplore said...

Well done got some nice information, keep going...

Anonymous said...

Hi,
I am getting below pasted exception with both IE, firefox. I am getting the exception even after adding browser path to Environment variables,
reinstalled firefox browser.
Please help how to get rid of this exception.

FAILED CONFIGURATION: @BeforeMethod login
com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet?
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:97)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:91)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:262)
at com.thoughtworks.selenium.HttpCommandProcessor.getBoolean(HttpCommandProcessor.java:335)
at com.thoughtworks.selenium.DefaultSelenium.isElementPresent(DefaultSelenium.java:503)
at com.ca.wily.apm.qa.shared.TessLogin.loginTess(TessLogin.java:25)
at com.ca.wily.apm.qa.M3Test.SecurityTest.login(SecurityTest.java:59)
... Removed 24 stack frames
SKIPPED CONFIGURATION: @AfterMethod logout
SKIPPED CONFIGURATION: @AfterClass closeBrowser

Mahesh Narayanan said...

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);

hh354 said...

It's a nice workaround but it doesn't work if you want to work cross browser and cross platform.

I'm working on a mac and when I use the windowMaximize from selenium he maximizes my window but the Firefox window stays at the bottom of the screen. Any idea?

Mahesh Narayanan said...

Hi Henry, It does support IE and firefox and should work on other browsers as well. I just tested it on both these browsers and it works without a flaw. I can't comment whether it will work on MAC though as I don't have access to one. BTW, Do you mean the selenium window is maximized and the test application browser window is minimized?
Can you try this on a PC? If it works on a PC then we can attribute this behavior to MAC.

hh354 said...

I'm currently working on a mac and on a mac you can't use Alt+Space to go to the menu bar to maximize.

It was weird, it maximized the window but didn't move it to (0,0) (the top left of the screen). The firefox window stayed under the selenium window. I've found a solution but you have to keep it in mind for further steps in your program:
selenium.getEval("window.moveTo(1,0)");

Mahesh Narayanan said...

Henry,
In MAC you have to do a CTRL-F2 acording to http://lifehacker.com/321595/access-the-dock-and-menu-bar-from-your-keyboard

can you please try this manually and let me know. If this works then you just need to change a few line in the code.

hh354 said...

It works with Ctrl+fn+F2 but:
In Firefox you can go to View -> Full Screen or just hit Shift + Cmd + F
But there's no maximize option

In Safari you neither have the full screen option or the maximize option.

I think it's too bad for trying this way.

Ramu B said...

Its great to see this blog with a valuable knowledge share...am not getting any word to explain, with simple word thx,
keep it up to share the knowledge .....tht It Mr Mahesh.

Svenem said...

Works well on Mac OS X for me:
selenium.getEval("window.moveTo(1,0)");
selenium.windowMaximize();


Just windowFocus() is not working.

Mahesh said...

Hi Mahesh,

Thanks for the nice tutorial. I followed all the steps in the demo when I run the test, I see there are errors in the below problem window as follows, could you please guide me ?:

1. The project was not built due to "c:\MyExclipseWorkSpace\dataDrivenTestProject\bin\Resources". Fix the problem, then try refreshing this project and building it since it may be inconsistent.

optimist said...

This doesnt seem to work if running selenium with singlewindow option. any pointers on how to maximize browser when using singlewindow option

Thanks
Vishal

Unknown said...

Hi Mahesh,

Appreciate all the effort and information shared with this blog, it was really helpful.
Is there a way out to use the @DataProvider feature without passing parameters?

Lets take an e.g. where I have more than 30 input parameters, so I put all the i/ps in an xls and I pass parameters as 'xls File Name', 'xls Sheet Name' and 'Row No' in the xls instead of calling all the 30 parameters. We are currently following this using QTP.

So would that be really possible using the @DataProvider method?
Please help.

ritesh thakur said...

Hi Mahesh,
Thanks for the article.
I have a query like can we use single data provider while reading the data from two different sheet(Sheet1 and Sheet2). If yes how?
Ca you please explain with the help of same example which you have shown here.
Thanks in advance. Please revert back it's urgent.
Regards
Ritesh

Anonymous said...

Why do you move down in the Menu when there is a Short Key ("x") that you can use?

robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyReleas(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);

This Code works fine on my system.

sandeepSG said...
This comment has been removed by the author.
sandeepSG said...

Hi, Why do u have to copy paste the code so much.

for (int i = 1; i<5;i++)
{
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);
}

Press down as many times as you want with a for loop :) Happy coding :)

Unknown said...

Hi,

I'm getting these error. Can you plz help me to fix the issues.

org.testng.TestNGException:
Cannot find class in classpath: DataProviderExample
at org.testng.xml.XmlClass.loadClass(XmlClass.java:76)
at org.testng.xml.XmlClass.init(XmlClass.java:68)
at org.testng.xml.XmlClass.(XmlClass.java:54)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:523)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:170)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:305)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

i'm awaiting for your reply.

Regards,,
Shiva

Unknown said...

Hi,

(Help)we have one opening for 'Core Selenium'(Exp more than 2+years) candidate. if any one having good knowledge on selenium with java. contact Anjaiah-09959264263

thanks
Anjaiah Karnati

JacobHarman said...
This comment has been removed by the author.