LoginSignup
0
0

More than 3 years have passed since last update.

Cloud9(EC2 Ubuntu)で、WebdriverIOを実行

Last updated at Posted at 2019-05-01

作業メモです。

  • Cloud9 ( Tokyo Region)
  • EC2: Ubuntu Server 18.04 LTS

基本的には下記の手順に沿って進めます。

ポイント

EC2 Ubuntu Server には Displayが無いので

「HEADLESSオプションをつけて実行」

言いたいのは、これだけです。

Firefoxインストール

$ sudo apt-get install xvfb firefox
$ firefox -v
Mozilla Firefox 66.0.3

xvfb(short for X virtual framebuffer)は、仮想ディスプレイ。

Nodejs インストール

Cloud9なら既にインストール済み

~/environment$ node -v
v10.15.3

作業ディレクトリ作成

$ mkdir webdriverio-test && cd webdriverio-test

Geckodriverをダウンロード

$ curl -L https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-linux64.tar.gz | tar xz

Geckodriverを起動

手順では、Port:4444 指定しているが、Defaultが4444みたいなので、シンプルに起動。

$ ./geckodriver 

Portを明示指定したいなら、下記。

$ ./geckodriver --port 4444

このままGeckdriverを起動したまま、別のコンソールを開いて作業する(重要)。

WebdriverIOをダウンロード(インストール)

$ cd webdriverio-test
$ npm install webdriverio

この辺り、Cloud9のエラーが出て、ごにょごにょしたかも知れない。。。

テストファイルを作成(test.js)

const { remote } = require('webdriverio');

(async () => {
    const browser = await remote({
        logLevel: 'error',
        path: '/',
        capabilities: {
            browserName: 'firefox'
        }
    });

    await browser.url('https://webdriver.io');

    const title = await browser.getTitle();
    console.log('Title was: ' + title);

    await browser.deleteSession();
})().catch((e) => console.error(e));

テスト実行 ... は失敗する。

$ node test.js

だが、DISPLAYがないのでエラーになる。

2019-05-01T13:25:32.239Z ERROR webdriver: Request failed due to unknown error: Process unexpectedly closed with status 1

Geckdriverには下記のメッセージが出力されてる。

Error: no DISPLAY environment variable specified

ブラウザオプション「headless」を追加する。

test.jsのCapabilitiesに、下記を追記する。

            "moz:firefoxOptions": {
              args: ['-headless']
            }

追記した結果は下記。カンマ区切りに注意。

const { remote } = require('webdriverio');

(async () => {
    const browser = await remote({
        logLevel: 'error',
        path: '/',
        capabilities: {
            browserName: 'firefox',
            "moz:firefoxOptions": {
              args: ['-headless']
            }
        }
    });

    await browser.url('https://webdriver.io');

    const title = await browser.getTitle();
    console.log('Title was: ' + title);

    await browser.deleteSession();
})().catch((e) => console.error(e));

再度テスト実行

$ node test.js

今度はうまくいくはずです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0