Home > Programming, QA, Test Automation > Using different emulators with Chrome in C#

Using different emulators with Chrome in C#

Hi guys, Today I will post a sample code that you can use to test different screens/mobile devices/browsers using Google Chrome browser with C#. You will need change the bold words for the devices and sizes that you want to simulate.

private static ChromeOpt SetCapabilities()
{
Map chromeOptions = new HashMap();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();  capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);


switch (whichCase) {


case "byDevice":
Map mobileEmulation = new HashMap();
mobileEmulation.put("deviceName", "Apple iPhone 4"); //Should be the same name showing in the Chrome Device Emulator
chromeOptions.put("mobileEmulation", mobileEmulation);
break;

case "CustomScreen":
Map deviceMetrics = new HashMap();
deviceMetrics.put("width", 900);
deviceMetrics.put("height", 600);
deviceMetrics.put("pixelRatio", 3.0);


Map 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.put("mobileEmulation", mobileEmulation);


break;

}

}
IWebDriver driver =new ChromeDriver(capabilities);
return driver;
}

You can look the list of user agents here:

http://www.useragentstring.com/pages/useragentstring.php

If you have any thoughts/suggestions please leave your comment here 🙂

Thank you guys !

Advertisements
  1. William
    February 19, 2015 at 10:27 pm

    Fantastic solution to a windows only client looking for a omni-channel solution in a >NET world!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: