A.Kaur
A.Kaur

Reputation: 11

Is there an alternative to using window().setSize in Selenium?

With latest Chrome Browser update, our UI tests for mobile browsers are failing on Chrome browser for windows.

I have been using this: driver.manage().window().setSize(dimension);

They are failing as UI elements do not match the expected on minimum chrome browser width.

But the site just loads fine on actual mobile devices on chrome browser.

Is there an alternative available in selenium which overrides the default minimum browser width set by chrome browser.

driver.manage().window().setSize(dimension);

I am trying to set the browser width to 385 but on Windows Chrome it automatically resets to 540 which causes the validations to fail

Upvotes: 1

Views: 971

Answers (1)

Fabiano Tarlao
Fabiano Tarlao

Reputation: 3232

I have worked with Selenium/ChromeDriver and Python, time ago. Hope this still applies.

I think that you need to change a bit the way you manage the mobile tests, not only for the faulty resolution. I encourage you to check if my experience applies to your case too.

In my case, setting the browser window resolution worked well with Chrome in desktop mode, but I had problems with the Chrome mobile mode. In mobile mode, I cannot set the resolution with setSize(), it was in fact ignored and the browser window size always is the screen resolution of the 'simulated' mobile phone.

Thinking about, it makes sense.

For this reason I encourage you to double check/verify that your selenium tests, in mobile mode, are effectively changing the browser window size--i.e., I fear you are always doing the web page rendering at the same resolution... the screen resolution of the (simulated) mobile phone.

For this reason, in my code, I used the setSize()code only for the desktop mode operations/tests.

For the Chrome mobile mode, my code creates a new ChromeDriver instance for each browser window resolution (x,y), by changing the mobile phone screen resolution to (x,y); you can do this with a ChromeDriver experimental option, code follows.

Java code for screen resolution x,y = 800,1280:

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 800);
deviceMetrics.put("height", 1280);
deviceMetrics.put("pixelRatio", 1.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);

Your code should stop the driver instance and re-create a new one for each tested resolution.

I found no way to do it online with the same instance :-)

Please share your findings,

Regards

P.s: I always used pixelRatio = 1.0, I don't remind the exact meaning but I infer that this is most reasonable value for modern devices.

P.s.2: The provided code is a "mapping" of my Python code, it is untested

Upvotes: 0

Related Questions