img

Selenium Mobile Emulation

I used to get questions from people asking about how they can automate the mobile browsers via Selenium. I guess everyone knows the answers is we can’t automate the Mobile Browsers or App with Selenium alone. The problem is Selenium needs a medium to communicate with the browser and you know it’s the Appium. Appium is the server which basically communicates with the browser and passes your Selenium commands to the device via JSON Wire Protocol.

But then I was thinking we can’t automate the mobile application but browsers? I know and I saw many manual testers are used to test their application in the mobile browser via Google Chrome mobile view as below.

 

So I thought there might be a way we can also change the Google Chrome browser in the mobile view with the different devices and can do the automation for the Google Chrome browser in mobile view without using Appium!

and definitely Google team come up with a solution for the same, so now you can at least automate your Chrome mobile browsers with Selenium. Let’s see how it works.

Basically, this thing comes up with the chromedriver which you’re using to automate your Chrome browser since a long. Chromedriver provides few properties to set the mobile device and you can get that mobile view directly in Google Chrome.  Look at the below code.

//Create a map to add device properties i.e. Device Name
Map<String, String> deviceProps = new HashMap<>();
deviceProps.put("deviceName", "iPhone X"); //Device Name should be same as in Chrome

//Create a map for the chromeoption and add the above device
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", deviceProps);

//Initialize the Capabilities and pass the chromeoptions and initialize the Chrome brower
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);

 

This is it, now you can easily automate your web application in iPhone X device without having Appium and obviously Mac device or iPhone device! But now question arise is these are the devices which are listed by Chrome but what about some custom devices which are not in chrome and you’re adding it manually sometimes, how we can add that here for automation? For this again, Chrome team come up with the solution, you just need to add the device details like you are adding in chrome to add your custom device. You need to add the basic things and the requirement for device testing like height, width, pixel ratio etc same you adding in chrome.

//Create a Map to define device attributes
Map<String, Object> deviceAttr = new HashMap<>();
deviceAttr.put("width", 768);
deviceAttr.put("height", 1024);
deviceAttr.put("pixelRatio", 2);

//Create a map to add device properties i.e. Device Details from above
Map<String, Object> deviceProps = new HashMap<>();
deviceProps.put("deviceMetrics", deviceAttr);
deviceProps.put("userAgent", "Mozilla/5.0 (iPhone; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");

//Create a map for the chromeoption and add the above device
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", deviceProps);

//Initialize the Capabilities and pass the chromeoptions and initialize thr Chrome brower
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);

This is cool!

But yet there are certain drawbacks as well of using this like you can’t get the mobile features like camera and galleries etc. and the working on Google Web Browser instead of Mobile Browser will also make difference in web app performance as well.

This is it! So now you don’t need Appium or any other extra tool for automating the mobile Chrome Browsers!

Thanks! #HappyTesting #Step2QA

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

It is main inner container footer text
Accessibility