在forSelenium的chromeOptions中一起指定解压缩的扩展目录和移动仿真

时间:2016-03-04 23:18:36

标签: selenium-chromedriver

我可以单独指定这两个并且它们正常工作:

https://sites.google.com/a/chromium.org/chromedriver/extensions(注意:我想要解压缩目录,而不是打包crx)

https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

因此,当两者结合使用时,我需要为前者创建一个new ChromeOptions()对象,为后者创建new HashMap()对象作为Chrome选项,在设置功能时,我只能设置一个就好像我设置后者将覆盖前者

1 个答案:

答案 0 :(得分:0)

我使用"扩展"接收base64编码扩展文件列表的参数。

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

try {
    chromeOptions.put("extensions", singletonList(Base64.encode(FileUtils.readFileToByteArray(new File(WebDriverManager
        .class.getResource("/extension_2_1_0.crx").toURI())))));
    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);
// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");
// setup ModHeader with headers
driver.executeScript(
"localStorage.setItem('profiles', JSON.stringify([{                " +
        "  title: 'Selenium', hideComment: true, appendMode: '',           " +
        "  headers: [                                                      " +
        "    {enabled: true, name: 'headerName', value: 'headerValue', comment: ''}, " +
         "  ],                                                              " +
         "  respHeaders: [],                                                " +
        "  filters: []                                                     " +
        "}]));                                                             ");
return driver;