'Switching between Mobile view & Desktop view of Chrome browser in Selenium
I have a requirement where I want to test the system in mobile view and desktop view, so it requires my code to be able to switch between the views mid way, like first it tests TC1 in mobile view then it tests TC1 in desktop view then again TC2 in mobile view and TC2 in desktop view and so on. When we initiate the browser it opens up in normal desktop view and I convert it to mobile view using the following code -
deviceMetrics.put("width", 414);
deviceMetrics.put("height", 1400);
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.setExperimentalOption("mobileEmulation", mobileEmulation);
Is there a way I can convert the browser to Desktop view in the same session?
Solution 1:[1]
Since you need no to switch between the desktop and mobile modes during the same test case the simplest way to resolve your problem is to pass some parameter with the test. I guess you are using TestNG or jUnit? Just pass Boolean parameter isMobileMode
In this case you can simply
if(isMobileMode){
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
}
there while all the rest lines will be not changed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prophet |
