简体   繁体   English

如何在流浪汉ubuntu-16.04中使用量角器运行无头angular2 e2e测试?

[英]How run headless angular2 e2e tests using protractor in vagrant ubuntu-16.04?

I am running ubuntu-16.04 using Vagrant and need to run angular2 end-to-end (e2e) tests in a gui-less environment for continuous integration. 我正在使用Vagrant运行ubuntu-16.04,并且需要在无GUI的环境中运行angular2端到端(e2e)测试以进行持续集成。 After scouring the angular documentation I have failed to find even any mention of what seems like should be a common use case. 在搜寻了有角度的文档之后,我什至没有发现任何提及似乎是一个常见的用例。

I have found a couple instruction sets coming close that utilize xvfb, but the lack of upfront documentation from angular on this makes me feel I am missing something obvious. 我已经找到了几个使用xvfb的指令集,但是由于缺少有关角度的前期文档,这使我感到自己缺少明显的东西。

http://www.tothenew.com/blog/protractor-with-jenkins-and-headless-chrome-xvfb-setup/ http://www.tothenew.com/blog/protractor-with-jenkins-and-headless-chrome-xvfb-setup/

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

Update I answered the question below with two different working solutions. 更新我用两种不同的可行解决方案回答了以下问题。

XVFB Based Solution: 基于XVFB的解决方案:

Standalone Selenium Server 独立硒服务器

Below is a working solution that uses a standalone selenium server taken mainly from www.tothenew.com. 以下是一个使用独立硒服务器的工作解决方案,该服务器主要来自www.tothenew.com。

Installs: 安装:

#Provision Java JDK
sudo apt-get install default-jdk -y

#Provision Protractor
sudo npm install protractor -g

#Webdriver update
sudo webdriver-manager update

#Exposing chromedriver
sudo ln /usr/lib/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.29 /usr/bin/chromedriver

sudo apt-get install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4 -y

#Provision Google Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install google-chrome-stable -y

sudo apt-get install xvfb gtk2-engines-pixbuf -y
sudo apt-get install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable -y
sudo apt-get install imagemagick x11-apps dbus-x11 -y

Create script: /etc/init.d/selenium 创建脚本:/etc/init.d/selenium

config.vm.provision "shell", inline: <<-SHELL
  echo -n                                             >   /etc/init.d/selenium
  echo '#!/bin/sh'                                    >>  /etc/init.d/selenium
  echo 'Xvfb -ac :99 -screen 0 1280x1024x16 &'        >>  /etc/init.d/selenium
  echo '#disown $1'                                   >>  /etc/init.d/selenium
  echo 'export DISPLAY=:99'                           >>  /etc/init.d/selenium
  echo 'webdriver-manager start /dev/null 2>&1'       >>  /etc/init.d/selenium

  sudo chmod +x /etc/init.d/selenium

SHELL

Update protactor.conf.js to use seleniumAddress: 更新protactor.conf.js以使用seleniumAddress:

exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './e2e/**/*.e2e-spec.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    seleniumAddress: 'http://localhost:4444/wd/hub',
    directConnect: false,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    beforeLaunch: function() {
        require('ts-node').register({
            project: 'e2e/tsconfig.e2e.json'
        });
    },
    onPrepare() {
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

To Run: 跑步:

  1. run /etc/init.d/selenium 运行/etc/init.d/selenium
  2. run ng e2e 运行ng e2e

Protractor Starts Selenium Server 量角器启动Selenium服务器

Based on the suggestion from Sudharsan below is another working solution. 根据Sudharsan的建议,以下是另一个可行的解决方案。 In this case protractor starts selenium server. 在这种情况下,量角器将启动硒服务器。

Installs: 安装:

#Provision Java JDK
sudo apt-get install default-jdk -y

#Provision Protractor
sudo npm install protractor -g

#Webdriver update
sudo webdriver-manager update

#Exposing chromedriver
sudo ln /usr/lib/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.29 /usr/bin/chromedriver

#Provision Google Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install google-chrome-stable -y

#Provision xvfb
sudo apt-get install -y xvfb
sudo apt-get install -y  xfonts-100dpi xfonts-75dpi xfonts-cyrillic  dbus-x11

Protractor config: 量角器配置:

exports.config = {
    specs: [
        './e2e/**/*.e2e-spec.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    directConnect: true,
    //getPageTimeout: 60000,  
    //allScriptsTimeout: 60000,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine2',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    beforeLaunch: function() {
        require('ts-node').register({
            project: 'e2e/tsconfig.e2e.json'
        });
    },
    onPrepare() {
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

To Run: 跑步:

xvfb-run -a -e /dev/stdout -s "-screen 0 2920x2580x24" ng e2e

Running Unit Tests Against Chrome Also Works 针对Chrome运行单元测试也可以

xvfb-run -a -e /dev/stdout -s "-screen 0 2920x2580x24" ng test --single-run

Headless Chromium Solution: 无头铬溶液:

Chrome now has a headless option. Chrome现在有一个无头的选择。 xvfb no longer required. xvfb不再需要。 The xvfb installs can be omitted. xvfb安装可以省略。

karma 业力

browsers: ['ChromeCI'],
customLaunchers: {
  ChromeCI: {
    base: 'Chrome',
    flags: ['--no-sandbox', '--headless', '--disable-gpu', '--remote-debugging-port=9222']
  }
},

protractor 量角器

capabilities: {
  'browserName': 'chrome',
  'chromeOptions': {
    'args': ['no-sandbox', 'headless', 'disable-gpu']
  }
},

Test can then be running normally: 然后测试可以正常运行:

ng test
ng e2e

It's very simple, Just follow the below steps. 这非常简单,只需执行以下步骤。

Step1: Install xvfb. 步骤1:安装xvfb。

enter the below command in terminal to install xvfb and its dependencies, 在终端中输入以下命令以安装xvfb及其依赖项,

 sudo apt-get install -y xvfb
 sudo apt-get install -y  xfonts-100dpi xfonts-75dpi xfonts-cyrillic  dbus-x11

Thats it. 而已。 Now you can trigger your protractor test using below command, 现在,您可以使用以下命令触发量角器测试,

xvfb-run -a -e /dev/stdout -s "-screen 0 2920x2580x24" protractor config.js

您还可以将docker作为轻量级解决方案签出: https : //hub.docker.com/r/trion/ng-cli-e2e/更详细的手册: https : //hub.docker.com/r/trion/ng -cli /

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

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