selenium/ WebdriverIO Chrome无头?

使用Chrome在无头模式下使用Selenium / WebdriverIO进行自动浏览器testing有可能吗?

据说铬 – 无头是现在的事情,但我无法得到他们的例子工作。 我希望Selenium有这个select吗?


我正在初始化WebdriverIO像这样:

const WebdriverIO = require('webdriverio'); let driver = WebdriverIO.remote({ desiredCapabilities: { browserName: browser, // "chrome" or "firefox" }, }); 

我开始使用selenium独立selenium :

 selenium-standalone start > /dev/null 2>&1 

WebdriverIO

以下是WebdriverIO的一个工作示例: https : //github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/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(); 

WebDriverJS

下面是WebDriverJs(WebDriver的官方JavaScript客户端)的示例: https : //github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts

基本思路:

 import * as webdriver from 'selenium-webdriver'; import * as chromeDriver from 'selenium-webdriver/chrome'; // 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 = new chromeDriver.Options(); options.setChromeBinaryPath(CHROME_BIN_PATH); options.addArguments( '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', ); const driver = new webdriver.Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); 

我自己还没有尝试过,但是可以从这个docker镜像下载 – 无头版本:

https://hub.docker.com/r/justinribeiro/chrome-headless/

或者自己构build(这需要几个小时,而且你需要大量的RAM :)) http://www.zackarychapple.guru/chrome/2016/08/24/chrome-headless.html

那么你应该可以在你的chrome启动脚本中指定–headless,并使用chromedriver,在开发邮件列表中根据这个问题: https ://groups.google.com/a/chromium.org/forum/# !话题/无头-dev /目录aAGFq8n_s6g

您可以使用HtmlUnitDriver()来实现Selenium的无头浏览器testing。

 driver = new HtmlUnitDriver(); driver.get(URL); String title = driver.getTitle(); System.out.println(title); 

但我明白你想用Chrome浏览器进行特定的无头浏览器testing,…..让我试着回到你身边。

除了HTML单元驱动程序,另一种有助于在非Gui模式下使用webdriver的方法是使用Linux的XVirtual frame buffer。 使用它你可以使用Chrome和Firefox驱动程序。 整个解决scheme包括Jenkins,Selenium Firefox驱动程序和Blazemeter在Linux上使用XVirtual帧缓冲区的描述如下: Jenkins中的无铅执行Seleniumtesting 。 当然你也可以使用Chrome驱动。