Showing posts with label web automation. Show all posts
Showing posts with label web automation. Show all posts

Friday, November 4, 2016

Mobile Emulation on Desktop using Selenium WebDriver for Chrome

I got a work from a client on web automation. But everything was working on mobile browser. No matter what way I try, I couldn't get it to work on my desktop browser. Then I found that I can simulate mobile web browsers using chrome webdriver for Selenium. I would like to share this with you, in case any of you are having the same problem.

ChromeDriver Official Website This is the website where I got my solution.

I am using Java language for my work. So I am gonna write the example on Java.


Simulating Google Nexus 5

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

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


public class ChromeMobileDemo {


    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
                           "tools\\chromedriver.exe");


        Map mobileEmulation = new HashMap();
        mobileEmulation.put("deviceName", "Google Nexus 5");

        Map chromeOptions = new HashMap();
        chromeOptions.put("mobileEmulation", mobileEmulation);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        WebDriver driver = new ChromeDriver(capabilities);



        driver.get("https://www.google.com");

        // wait 60 seconds before closing the window
        try {
            driver.wait(60000);
        } catch (InterruptedException e) {
            // do nothing
        }


        driver.close();
    }
}

Simulating iPhone 6

Just change mobileEmulation.put("deviceName", "Google Nexus 5") line to mobileEmulation.put("deviceName", "iPhone 6");


Simulate Other devices

You can select any device that you have on your Chrome's Developer Settings. Just put the same of the device.