web-dev-qa-db-ja.com

Selenium / WebdriverIO Chrome headless?

Selenium/ WebdriverIO using Chrome headlessモードでブラウザの自動テストを行うことはできますか?

おそらく Chrome --headless は今では問題になっていますが、私は彼らの例を動作させることはできません。私は、セレニウムにこのオプションがあることを望んでいましたか?


私は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
16
mpen

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

私はまだ自分でこれを試しませんでしたが、このドッカーイメージから--headless buildをダウンロードできます。

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

または自分でビルドします(これには数時間かかり、多くのRAM :)が必要です) http://www.zackarychapple.guru/chrome/2016/08/24 /chrome-headless.html

次に、chrome起動スクリプトに--headlessを指定し、devメーリングリストの次の質問に応じてchromedriverを使用するだけです。 https://groups.google .com/a/chromium.org/forum /#!topic/headless-dev/aAGFq8n_s6g

2
cvakiitho

引数をString '--headless'の配列として設定するchromeOptionsを追加することにより、ドライバーに機能を追加できます。

 capabilities: [{

        maxInstances: 1,

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

    }],
1
Biswajeet gope

Wdio.conf.jsファイルの機能を使用できます

capabilities: [{

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

HtmlUnitDriver()を使用して、Seleniumでヘッドレスブラウザーテストを実行できます。

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

しかし、クロムを使用した特定のヘッドレスブラウザテストが必要であることは理解しています。

0
Dharam

HTMLユニットドライバーに加えて、非GuiモードでWebドライバーを使用するのに役立つ別のアプローチは、Linux用のXVirtualフレームバッファーを使用することです。これを使用すると、ChromeとFirefoxドライバーの両方を利用できます。Jenkins、Selenium Firefoxドライバー、およびLinuxでXVirtualフレームバッファーを使用するBlazemeterを含むソリューション全体を以下に説明します。 Jenkinsでのセレンテストのヘッドレス実行 。もちろん、代わりにChromeドライバーを使用できます。

0