9

I've been using the following example to maximize my windows in my WebDriver tests, I upgraded to Firefox 7, and the script quit working. I don't get an error, the window just does not maximize, wondering if anyone else has seen this or know why it's no longer working, or another way to do it..

my current code that worked before FireFox 7...

public static final String MAXIMIZE_BROWSER_WINDOW = "if (window.screen) {window.moveTo(0, 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};";

public static Object maximizeBrowserWindow(WebDriver driver) {
    return executeJavascript(driver, MAXIMIZE_BROWSER_WINDOW);
}

private static Object executeJavascript(WebDriver driver, String script){
    JavascriptExecutor js=(JavascriptExecutor) driver;
return js.executeScript(script);
}
Uooo
  • 6,204
  • 8
  • 36
  • 63
Green
  • 1,405
  • 4
  • 21
  • 27

5 Answers5

8

Firefox 7 disabled the ability to modify the main window via JavaScript. The issue report can be found in the Mozilla bug tracker. There have been some discussions of workarounds, on the WebDriver users mailing list, but none of them are particularly pretty.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
4

This is how it is done for the Firefox browser (as of the selenium-java-2.25.0 jar):

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
1

The link in the accepted answer does not provide the solution. Here it is (tested in Firefox 11):

String JS_GET_MAX_WIDTH = 
        "return (window.screen ? window.screen.availWidth : arguments[0]);";
String JS_GET_MAX_HEIGHT = 
        "return (window.screen ? window.screen.availHeight : arguments[0]);";

Toolkit toolkit = Toolkit.getDefaultToolkit();
int width = ((Long) executor().executeScript(
        JS_GET_MAX_WIDTH, 
        (int)toolkit.getScreenSize().getWidth())).intValue();
int height = ((Long) executor().executeScript(
        JS_GET_MAX_HEIGHT, 
        (int)toolkit.getScreenSize().getHeight())).intValue();
org.openqa.selenium.Dimension screenResolution = 
        new org.openqa.selenium.Dimension(width, height);
driver.manage().window().setSize(screenResolution);

The JavaScript environment is queried because it returns values which are relative to the browser's current monitor. The Java Toolkit is used as a fallback, as it returns the screen size of the O/S's primary monitor (not necessarily what you want). The actual resize method is provided by WebDriver's Window interface.

Joe Coder
  • 4,498
  • 31
  • 41
1

Here is how I ended up doing it, which works fine, but seemed a hack.

Set<String> handles = driver.getWindowHandles();
String script = "if (window.screen){var win = window.open(window.location); win.moveTo(0,0);win.resizeTo(window.screen.availWidth,window.screen.availHeight);};";
((JavascriptExecutor) driver).executeScript(script);
Set<String> newHandles = driver.getWindowHandles();
newHandles.removeAll(handles);
driver.switchTo().window(newHandles.iterator().next());
Green
  • 1,405
  • 4
  • 21
  • 27
0

Did you try using WebDriverBackedSelenium object? I have always been using selenium object to maximize the browser window.

Selenium selenium = new WebDriverBackedSelenium(driver, url);
selenium.windowMaximize();
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • Won't work. Under the hood, selenium is just calling those JavaScript window methods. – Joe Coder Apr 19 '12 at 16:05
  • You are right. Didn't know Mozilla deprecated window.resizeTo() firefox 7 onwards and selenium.windowMaximize() calls that JS method. – nilesh Apr 20 '12 at 00:20