是否可以在无头模式下使用Chrome进行Selenium / WebdriverIO自动浏览器测试?
据说Chrome --headless现在已成为一件事,但我无法让他们的榜样奏效。我希望Selenium可以选择这个吗?
我正在初始化WebdriverIO,如下所示:
const WebdriverIO = require('webdriverio');
let driver = WebdriverIO.remote({
desiredCapabilities: {
browserName: browser, // "chrome" or "firefox"
},
});
我正在使用selenium-standalone启动Selenium:
selenium-standalone start > /dev/null 2>&1
答案 0 :(得分:22)
以下是WebdriverIO的一个工作示例:https://github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/src/index.ts
基本理念:
@Test
public void extendingCustomComparatorToAllowToCompletelyIgnoreCertainAttributes() throws JSONException {
// AttributeIgnoringComparator completely ignores some of the expected attributes
class AttributeIgnoringComparator extends CustomComparator{
private final Set<String> attributesToIgnore;
private AttributeIgnoringComparator(JSONCompareMode mode, Set<String> attributesToIgnore, Customization... customizations) {
super(mode, customizations);
this.attributesToIgnore = attributesToIgnore;
}
protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException {
Set<String> expectedKeys = getKeys(expected);
expectedKeys.removeAll(attributesToIgnore);
for (String key : expectedKeys) {
Object expectedValue = expected.get(key);
if (actual.has(key)) {
Object actualValue = actual.get(key);
compareValues(qualify(prefix, key), expectedValue, actualValue, result);
} else {
result.missing(prefix, key);
}
}
}
}
String expected = "{\"timestamp\":1234567, \"a\":5}";
String actual = "{\"a\":5}";
JSONAssert.assertEquals(expected, actual,
new AttributeIgnoringComparator(JSONCompareMode.LENIENT,
new HashSet<>(Arrays.asList("timestamp")))
);
}
以下是WebDriverJs(WebDriver的官方JavaScript客户端)的工作示例:https://github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts
基本理念:
import * as webdriverio from 'webdriverio';
// Headless is supported in Chrome >= 58. Not currently stable, so using dev
// build.
const CHROME_BIN_PATH = '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome';
const options = {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
binary: CHROME_BIN_PATH,
args: [
'headless',
// Use --disable-gpu to avoid an error from a missing Mesa
// library, as per
// https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
'disable-gpu',
],
},
},
};
webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.getTitle().then(title => {
console.log({ title });
})
.end();
答案 1 :(得分:2)
我自己也没试过,但你可以从这个docker图片下载--headless build:
https://hub.docker.com/r/justinribeiro/chrome-headless/
或自己构建(这需要几个小时,你需要大量的RAM :)) http://www.zackarychapple.guru/chrome/2016/08/24/chrome-headless.html
然后你应该能够只为你的chrome启动脚本指定--headhead,并使用chromedriver,根据这个问题在dev邮件列表中: https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/aAGFq8n_s6g
答案 2 :(得分:2)
您可以使用wdio.conf.js文件中的功能
capabilities: [{
maxInstances: 1,
browserName: 'chrome',
'goog:chromeOptions': {
args: ["--headless", "user-agent=...","--disable-gpu","--window-size=1440,735"]
}
答案 3 :(得分:0)
您可以使用HtmlUnitDriver()通过Selenium实现无头浏览器测试。
driver = new HtmlUnitDriver();
driver.get(URL);
String title = driver.getTitle();
System.out.println(title);
但我知道你想要使用chrome进行特定的无头浏览器测试,.....让我试着回复你。
答案 4 :(得分:0)
除了HTML单元驱动程序之外,另一种有助于在非Gui模式下使用webdriver的方法是使用Linux的XVirtual帧缓冲区。使用它您可以使用Chrome和Firefox驱动程序。整个解决方案包括Jenkins,Selenium Firefox驱动程序和在Linux上使用XVirtual帧缓冲区的Blazemeter,如下所述:Headless Execution of Selenium Tests in Jenkins。当然,您可以使用Chrome驱动程序。
答案 5 :(得分:0)
您可以通过添加chromeOptions来为驱动程序添加功能,该控件将参数设置为字符串'--headless'
的数组。
capabilities: [{
maxInstances: 1,
browserName: 'chrome',
chromeOptions: {
args: ['--headless'],
},
}],