Austin France
Austin France

Reputation: 2501

chrome webdriver set viewport below 500px?

In my selenium based tests, I am setting the window size to 400(w) 719(h) to create an internal viewport size of 400x640. Most of my tests are based on that size though some use other sizes.

Dimension size = new Dimension(400, 719);
driver.manage().window().setSize(size);

More recent versions of chrome fail to set the window size to 400 pixels width, instead, the resulting window width is around 500px (though it varies).

https://bugs.chromium.org/p/chromedriver/issues/detail?id=2639 (bug, claims its fixed, but seems not)

So instead I want to set the internal viewport size.

How do I do this in selenium/chrome webdriver?

Upvotes: 2

Views: 2999

Answers (1)

S A
S A

Reputation: 1813

You can take a look at chromedriver Moblie Emulation.

As mentioned there,

java example

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);

Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);

Upvotes: 1

Related Questions