agabrys
agabrys

Reputation: 9116

How to set browser client area size using Selenium WebDriver?

I found how to set browser window size at Selenium Issue Management system (see Browser window control #174):

Window window = driver.manage().window();
window.setPosition(new Point(0, 0));
window.setSize(new Dimension(width, height));

But this solution sets size for whole window (title bar, bookmarks bar, borders etc.), so client area has different sizes in different browsers. Is there any way to simply set the same value for popular browsers (Chrome, FireFox, Internet Explorer, Opera and Safari)? It will be great if I don't need to eval JavaScript.

Upvotes: 5

Views: 5715

Answers (1)

Florent B.
Florent B.

Reputation: 42518

It would be easier with a piece of Javascipt, but since you asked without:

Dimension win_size = driver.manage().window().getSize();
WebElement html = driver.findElement(By.tagName("html"));
int inner_width = Integer.parseInt(html.getAttribute("clientWidth"));
int inner_height = Integer.parseInt(html.getAttribute("clientHeight"));

// set the inner size of the window to 400 x 400 (scrollbar excluded)
driver.manage().window().setSize(new Dimension(
    win_size.width + (400 - inner_width),
    win_size.height + (400 - inner_height)
));

And with a piece of Javascript just in case:

ArrayList padding = (ArrayList)((JavascriptExecutor) driver).executeScript(
  "return [window.outerWidth-window.innerWidth, window.outerHeight-window.innerHeight];");

// set the inner size of the window to 400 x 400 (scrollbar included)
driver.manage().window().setSize(new Dimension(
    (int)(400 + (Long)padding.get(0)),
    (int)(400 + (Long)padding.get(1))
));

Upvotes: 6

Related Questions