2016-03-04 33 views
0

指定解壓擴展目錄和移動仿真在一起,我可以分別指定這兩個,他們都做工精細:在chromeOptions爲硒

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選項,在設置功能時,我只能設置一個,就好像我設置後者會覆蓋前者

回答

0

我在使用接收base64編碼擴展文件列表的「extensions」參數做這件事時有點成功。

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;