Priyank Thakkar
Priyank Thakkar

Reputation: 4852

Set viewport size for a browser

I was playing with a webapplication page developed by me using selenium.

Using selenium JavaScriptExecutor I executed following script.

executor.executeScript("return window.innerWidth");
executor.executeScript("return window.innerHeight");

to my surprise, the so called viewport size came different from Chrome, IE & Firefox.

Chrome: 1366x667 IE: 1366x667 Firefox: 1366x657

Then I realized that for body and main div, right next to body I gave css style width and height as 100% which in turn effects the view port size. As different borwsers are having different size of toolbars and menu, this 100% value changes when actual page is rendered.

So I used window.resizeTo(w,h) for setting a common viewport size. But I realized it doesn't work with modern browsers until and unless window is opened by the same script.

So I used selenium's

driver.manage().window().setSize(new Dimension(w,h));

yet I am not able to set the common view port size.

Please help to find, is it possible to set common viewport size using selenium? Do let me know, if you need anymore info.

I executed following line for all browsers:

driver.manage().window().getSize();

Answer I got is 1382x744

So I believe this about the overall browser window, not only rendering area. And I got confused that based on the difference of innerWidth how to calculate the browser window's new size?

PS: I have all the browsers upgraded to latest version (IE is version 11 as I am on windows 7), selenium version I am using is 2.46.x

Upvotes: 3

Views: 2628

Answers (1)

Baker Alhindi
Baker Alhindi

Reputation: 1

"to my surprise, the so called viewport size came different from Chrome, IE & Firefox."

It should not be surprising that the view port size is different on all browsers, Safari even has a different one as well. When you are calling window.innerHeight it only counts the browsers inner content. So anything outside of it is not counted, the area where you enter your URL is entered is not counted for example. So each browser uses a different amount of maximum height by their tabs, and Global address bar.

When you use driver.manage().window().setSize(new Dimension(w,h)); and set it to 1920x1080 for example then all browsers will be set to that size, but of course they will have different inner height being used.

If you want the entire browser to render as 1920x1080 then I would suggest with

using Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(250);    
robot.keyRelease(KeyEvent.VK_F11);

but I would advise against it, since a more valid test is to use the normal inner height of the browser, since most user do not press F11 and view their content at full screen.

Upvotes: 0

Related Questions