UI自動化テストのHeadless browserコンテナ化

6033 ワード

目的

  • E 2 E 2テスト実行中にUIインタフェース
  • に依存しない
  • *nixシステムで
  • を実行できます.
  • なぜPhantomJSを使用しないのか、ProtractorはPhantomJSを使用して
  • をテストすることを推奨していません.
    We recommend against using PhantomJS for tests with Protractor. There are many reported issues with PhantomJS crashing and behaving differently from real browsers.
    

    Docker


    インストール

  • システムに必要なインストールパッケージをダウンロードし、アドレスをダウンロードします.https://www.docker.com/products/docker
  • 登録https://hub.docker.com/アカウント
  • pullを使用してdockerミラーprotractor-headlessを取得します.このプロセスは遅くなり、docker hubアドレスを辛抱強く待ちます.https://hub.docker.com/r/webnicer/protractor-headless/git hubアドレス:https://github.com/jciolek/docker-protractor-headless
  • docker pull webnicer/protractor-headless
    

    E 2 EのProtractor


    可視化運転E 2 Eテスト

  • 取付Protractor
  • npm install -g protractor
    
  • 更新webdriver-manager
  • webdriver-manager update
    
  • conf.jsプロファイル
  • を作成する.
    // conf.js
    exports.config = {
      framework: 'jasmine',
      seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
      specs: ['**/**.js'],
      capabilities: {
        browserName: 'chrome'
      },
      jasmineNodeOpts: {
        showColors: true,
      }
    };
    
  • test-spec.jsテストスクリプト
  • を作成
    describe('angularjs homepage todo list', function() {
      it('should add a todo', function() {
        browser.get('https://angularjs.org');
    
        element(by.model('todoList.todoText')).sendKeys('write first protractor test');
        element(by.css('[value="add"]')).click();
    
        var todoList = element.all(by.repeater('todo in todoList.todos'));
        expect(todoList.count()).toEqual(3);
        expect(todoList.get(2).getText()).toEqual('write first protractor test');
    
        // You wrote your first test, cross it off the list
        todoList.get(2).element(by.css('input')).click();
        var completedAmount = element.all(by.css('.done-true'));
        expect(completedAmount.count()).toEqual(2);
      });
    });
    
  • E2E :protractor conf.jsを実行すると、chromeブラウザが起動し、コンソールに対応する実行結果
  • が表示されます.
    ➜  protractorHeadless git:(master) ✗ protractor conf.js
    [17:23:41] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub
    [17:23:41] I/launcher - Running 1 instances of WebDriver
    Started
    .
    
    
    1 spec, 0 failures
    Finished in 19.912 seconds
    [17:24:01] I/launcher - 0 instance(s) of WebDriver still running
    [17:24:01] I/launcher - chrome #01 passed
    
  • E 2 E 2の可視化テストが完了
  • Headless運転E 2 Eテスト

  • 次の内容を実行可能プログラム、shellファイル(unix)、batファイル(windows)
  • として保存します.
    #!/bin/bashdocker run -it --privileged --rm --net=host -v /dev/shm:/dev/shm -v $(pwd):/protractor webnicer/protractor-headless $@
    
  • はコンソールに入り、protractor.sh --versionを入力してバージョン番号を表示し、調製に成功したことを確認します.必ずdockerサービス
  • を起動してください.
  • conf.jsファイルの内容を変更し、dockerミラー内部のselenium server
  • を有効にします.
    // conf.js
    exports.config = {
      framework: 'jasmine',
    //  seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
      specs: ['**/**.js'],
      capabilities: {
        browserName: 'chrome'
      },
      jasmineNodeOpts: {
        showColors: true,
      }
    };
    
  • protractorのスクリプトルートディレクトリに入り、protractor.sh conf.jsを実行し、chromeブラウザを起動せず、コンソールに対応する実行結果
  • を表示する.
    ➜  protractorHeadless git:(master) ✗ protractor.sh conf.js
    [09:31:08] I/local - Starting selenium standalone server...
    [09:31:08] I/launcher - Running 1 instances of WebDriver
    [09:31:09] I/local - Selenium standalone server started at http://192.168.65.2:37226/wd/hub
    Started
    .
    
    
    1 spec, 0 failures
    Finished in 11.403 seconds
    [09:31:23] I/local - Shutting down selenium standalone server.
    [09:31:23] I/launcher - 0 instance(s) of WebDriver still running
    [09:31:23] I/launcher - chrome #01 passed
    
  • E 2 EのHeadlessテスト完了
  • 詳しく説明する


    Dockerfileファイル

    FROM node:slim
    MAINTAINER Yuanjie
    WORKDIR /tmp
    RUN npm install -g protractor mocha jasmine && \
        webdriver-manager update && \
        apt-get update && \
        apt-get install -y xvfb wget openjdk-7-jre && \
        wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
        dpkg --unpack google-chrome-stable_current_amd64.deb && \
        apt-get install -f -y && \
        apt-get clean && \
        rm google-chrome-stable_current_amd64.deb && \
        mkdir /protractor
    ADD protractor.sh /protractor.sh
    # Fix for the issue with Selenium, as described here:
    # https://github.com/SeleniumHQ/docker-selenium/issues/87
    ENV DBUS_SESSION_BUS_ADDRESS=/dev/null
    WORKDIR /protractor
    ENTRYPOINT ["/protractor.sh"]
    

    ミラーのプライマリ・プロビジョニングの説明
  • 取得基準ミラー:node:slim
  • 取付protractor,mocha,jasmine:E 2 Eテスト実行に必要な
  • 更新driver:webdriver-manager update
  • Xvfb:apt-get install-y xvfb,headlessのコアをインストールし、仮想メモリを使用してUI表示
  • をシミュレートします.
  • wget,jdk
  • をインストール
  • wgetを使用してchromeのdebバージョン
  • をダウンロード
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    
  • dpkgを使用してchrome
  • をインストール
  • Xvfbの運転パラメータを追加:ADD protractor.sh /protractor.shprotractor.shファイル内容
  • #!/bin/bash
    xvfb-run --server-args='-screen 0 1280x1024x24' protractor $@
    

    ソースアドレス:https://github.com/aimer1124/protractor-headless

    リファレンス

  • Headless Browser Testing With Xvfb
  • docker hub protractor-headless
  • Protractor browser setting up
  • XVFB