testergirl
testergirl

Reputation: 121

Selenium - Mobile Emulation - how do I add user Agent to Chrome options while automating the emulator?

Below are the capabilities I added. I am getting a Google reCAPTCHA in my website which can be trespassed by adding user agent.

But even after the addition of user agent I am still getting the captcha. Is there another way to add it?

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

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

chromeOptions.put("args",
                  Arrays.asList("disable-bundled-ppapi-flash",
                  "disable-extensions",
                  "profile-directory=Default",
                  "disable-plugins-discovery",
                  "--user-agent=" + userAgent));

ChromeOptions co = new ChromeOptions();
co.addArguments("mobileEmulation="+mobileEmulation);

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

System.setProperty("webdriver.chrome.driver", RunConfig.CHROME_DRIVER_EXE);

driver = new ChromeDriver(capabilities);

Upvotes: 3

Views: 14322

Answers (3)

Manasi
Manasi

Reputation: 19

public class DeviceEmulation {

    @Test
    public void deviceemulation() {
        Map<String, String> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "Pixel 2");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("mobileEmulation", mobileEmulation);
        options.addArguments("remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get(helper.Pages.Home);
        Demohelper.pause();
    }
}

Upvotes: 0

Pushparaj
Pushparaj

Reputation: 120

public class mobileEmulatorTest {

    public static WebDriver driver;

    @Before
    public void setUp(){
        WebDriverManager.chromedriver().setup();
    }

    @Test
    public void emulatorTest() throws InterruptedException {
        Map<String, String> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "iPhone 6");

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

        driver = new ChromeDriver(chromeOptions);
        driver.get("https://www.google.co.uk");
    }
}

Add WebDiver Manager dependency https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager

Upvotes: 0

Amit Jain
Amit Jain

Reputation: 4597

You can use the below configuration for mobile emulation in the Chrome web browser:

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 1078);
deviceMetrics.put("height", 924);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 8.0.0;" +
"Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML,
like Gecko) " +
"Chrome/67.0.3396.99 Mobile Safari/537.36");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);

Instead of add argument setExpermentalOption to be used

// co.addArguments("mobileEmulation=" + mobileEmulation);
co.setExperimentalOption("mobileEmulation", mobileEmulation);

Upvotes: 2

Related Questions