webdriver firefox 7 maximize window
Asked Answered
T

5

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);
}
Turfy answered 25/10, 2011 at 20:37 Comment(3)
BTW: I don't think you actually used Netscape 7 (which was released in 2002). You mean Firefox 7, don't you?Monarda
:). thank you. I do that when I'm searching for it too, I have no idea why my brain swaps those.. Yes I mean firefox.Turfy
No problem. I corrected the title.Monarda
L
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.

Lytle answered 26/10, 2011 at 15:36 Comment(0)
B
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();
Beaman answered 23/8, 2012 at 20:6 Comment(0)
E
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.

Elise answered 20/4, 2012 at 9:21 Comment(0)
T
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());
Turfy answered 20/4, 2012 at 13:29 Comment(0)
D
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();
Developing answered 25/10, 2011 at 22:49 Comment(2)
Won't work. Under the hood, selenium is just calling those JavaScript window methods.Elise
You are right. Didn't know Mozilla deprecated window.resizeTo() firefox 7 onwards and selenium.windowMaximize() calls that JS method.Developing

© 2022 - 2024 — McMap. All rights reserved.