简体   繁体   English

Selenium / WebdriverIO Chrome无头?

[英]Selenium/WebdriverIO Chrome headless?

Is it possible to do automated browser testing with Selenium/ WebdriverIO using Chrome in headless mode? 在无头模式下使用Chrome 可以使用Selenium / WebdriverIO进行自动浏览器测试吗?

Supposedly Chrome --headless is a thing now, but I can't get their example working. 据说Chrome - 无头是现在的事情,但我不能让他们的榜样有效。 I was hoping Selenium had an option for this? 我希望Selenium可以选择这个吗?


I'm initializing WebdriverIO like so: 我正在初始化WebdriverIO,如下所示:

const WebdriverIO = require('webdriverio');

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

And I'm starting Selenium using selenium-standalone : 我正在使用selenium-standalone启动Selenium:

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

WebdriverIO WebdriverIO

Here is a working example with WebdriverIO: https://github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/src/index.ts 以下是WebdriverIO的一个工作示例: https//github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/src/index.ts

The basic idea: 基本理念:

 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

Here is a working example with WebDriverJs (the official JavaScript client to WebDriver): https://github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts 下面是WebDriverJs(WebDriver的官方JavaScript客户端)的一个工作示例: https//github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts

The basic idea: 基本理念:

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();

I did not try this myself yet, but you can download --headless build from this docker image: 我自己还没试过,但你可以从这个docker图像下载--headless build:

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

or build it yourself (this takes few hours, and you need a lot of RAM :) ) http://www.zackarychapple.guru/chrome/2016/08/24/chrome-headless.html 或者自己构建(这需要几个小时,你需要大量的内存:)) http://www.zackarychapple.guru/chrome/2016/08/24/chrome-headless.html

Then you should be able to just specify --headless to your chrome launch script, and use chromedriver, acording to this question in dev mailing list: https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/aAGFq8n_s6g 那么你应该只能指定 - 无铬你的chrome启动脚本,并使用chromedriver,根据这个问题在dev邮件列表中: https ://groups.google.com/a/chromium.org/forum/# !话题/无头-dev /目录aAGFq8n_s6g

You can add capabilities to your driver by adding the chromeOptions which sets the arguments as an array of String '--headless' . 您可以通过添加chromeOptions来为驱动程序添加功能,chromeOptions将参数设置为String '--headless'的数组。

 capabilities: [{

        maxInstances: 1,

        browserName: 'chrome',
        chromeOptions: {
            args: ['--headless'],
        },

    }],

You can use capabilities in wdio.conf.js file 您可以使用wdio.conf.js文件中的功能

capabilities: [{

    maxInstances: 1,
    browserName: 'chrome',
    'goog:chromeOptions': { 
         args: ["--headless", "user-agent=...","--disable-gpu","--window-size=1440,735"]
    }

You can use HtmlUnitDriver() to achieve headless browser test with Selenium. 您可以使用HtmlUnitDriver()通过Selenium实现无头浏览器测试。

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

But i understand you want specific headless browser test with chrome, .....let me try and get back to you. 但我知道你想要使用chrome进行特定的无头浏览器测试,.....让我试着回复你。

Besides HTML Unit driver, another approach that helps to use webdriver in non Gui mode is to use XVirtual frame buffer for Linux. 除了HTML单元驱动程序之外,另一种有助于在非Gui模式下使用webdriver的方法是使用Linux的XVirtual帧缓冲区。 Using it you may utilize both Chrome and Firefox drivers. 使用它您可以使用Chrome和Firefox驱动程序。 The whole solution, that includes Jenkins, Selenium Firefox driver and Blazemeter with using XVirtual frame buffer on Linux is described here: Headless Execution of Selenium Tests in Jenkins . 整个解决方案,包括Jenkins,Selenium Firefox驱动程序和Blazemeter在Linux上使用XVirtual帧缓冲区在这里描述: 在Jenkins中无头执行Selenium测试 Of course you may use Chrome driver instead. 当然,您可以使用Chrome驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM