Moving With Automation

Mobile Emulation In Chromedriver

Tags: chromedriver selenium mobile

Table of Contents

ChromeDriver 2.11 release has the mobile emulation support using Chrome Dev Tools. I have been waiting for this feature since long.

This feature is really handy to run your selenium tests against different mobile screen sizes with minimal setup. This does not provide you 100% emulation but atlest it will give you some fair idea about the compatibility of your application with different mobile screen size.

This is how we can emulate different devices using the Java code.

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MobileEmulation {
 static DesiredCapabilities  capabilities;
 static String DeviceName;
 public static void main(String[] args) {
  //some Sample Devices. Complete list can be found here:
  //pick any of the device

//  DeviceName = "Google Nexus 5";
//  DeviceName = "Samsung Galaxy S4";
//  DeviceName = "Samsung Galaxy Note 3";
//  DeviceName = "Samsung Galaxy Note II";
//  DeviceName = "Apple iPhone 4";
  DeviceName = "Apple iPhone 5";
//  DeviceName = "Apple iPad 3 / 4";

  String ChromeDriverPath = System.getProperty("user.dir") + "/lib/chromedriver.exe";
  System.setProperty("", ChromeDriverPath);

  Map<String, String> mobileEmulation = new HashMap<String, String>();
  mobileEmulation.put("deviceName", DeviceName);

  Map<String, Object> chromeOptions = new HashMap<String, Object>();
  chromeOptions.put("mobileEmulation", mobileEmulation);

  capabilities =;
  capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
  WebDriver driver = new ChromeDriver(capabilities);

comments powered by Disqus
Table of Contents