Open Chrome with Toggle Device Toolbar Activated

2020-06-29 08:46发布

问题:

I'm working with java in Selenium, and I have to automaticed some tests, some how needs use Chrome Headless and Mobile Emulation. I know how to open chrome with "--auto-open-devtools-for-tabs" and with "mobileEmulation", but i need to open it with Toggle Device Toolbar activated. This mode, emulate the touch interactions events, replacing the click with a touch. How can i do start Chrome wiht this mode? (I´m using ChromeDriver v.2.358161)

Here's my code:

(...)
        Map<String, String> mobileEmulation = new HashMap<String, String>();
        mobileEmulation.put("deviceName", "Galaxy S5");
        ChromeOptions headlessOptions = new ChromeOptions();
        headlessOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, headlessOptions);
        driver = new ChromeDriver(capabilities);
(...)

What new argument i must to add?

(I made a robot press Ctrl+shift+M but this doesn´t works with Chrome Headless).

protected void pressModoResponsive() throws AWTException {
        pressF12();
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_SHIFT);
        waitSleep(1);
        robot.keyPress(KeyEvent.VK_M);
        waitSleep(1);
        robot.keyRelease(KeyEvent.VK_M);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }

回答1:

I have the same problem. I couldn't make WebDriver simulate a mobile device in selenium.

The workaround is being to add the argument: --auto-open-devtools-for-tabs, then switch the device to mobile and leave the session saved with--user-data-dir