简体   繁体   English

Karma 使用 Chrome 的 Docker 映像进行测试

[英]Karma tests using a Docker image of Chrome

For tests on CI server I want to use an image of Chrome instead of PhantomJS.对于 CI 服务器上的测试,我想使用 Chrome 的图像而不是 PhantomJS。 I can do this with and without puppeteer but both require me to install chrome-stable package on the server.我可以在有和没有 puppeteer 的情况下执行此操作,但两者都要求我在服务器上安装 chrome-stable 软件包。 Thus I want a more lightweight method hence the use of the docker image.因此我想要一个更轻量级的方法,因此使用 docker 图像。

For Karma, according to the docs, I must specify a script for the browser if i want to use a custom browser.对于 Karma,根据文档,如果我想使用自定义浏览器,我必须为浏览器指定一个脚本。 Also Karma will pass 1 argument to this script, the url. Karma 也会向这个脚本传递 1 个参数,即 url。 For this requirement I also pulled the browserless docker image browserless quickstart .对于这个要求,我还拉了 browserless docker image browserless quickstart

I have created the script and can tun the Karma test but it looks like the karma enters failed state before the script is finished executing.我已经创建了脚本并且可以调整 Karma 测试,但看起来 karma 在脚本执行完成之前进入失败状态。 Any ideas on where I am going wrong here.关于我在这里出错的任何想法。 The container runs as expected and curl command to localhost:3000 returns what is expected.容器按预期运行,对 localhost:3000 的 curl 命令返回预期内容。

Custom script自定义脚本

#!/bin/bash

set -euxo pipefail

URL="$1"
# http://localhost:9876/?id=30931514 this is the argument passed in by Karma
# Change the port to the port the docker container exposes 3000
DOCKER_URL=$( echo "$URL" | sed s/9876/3000/g )

killDockerContainer(){
 echo 'Killing Docker Container'
 docker rm -f browserless
}

trap "killDockerContainer; exit 0" EXIT


echo "Launching browserless: $DOCKER_URL"

docker run   -d   -p 3000:3000   --shm-size 2gb   --name browserless   --restart always   -e "DEBUG=browserless/chrome"   -e "MAX_CONCURRENT_SESSIONS=10" browserless/chrome 

Output from test run测试运行的输出

3 01 2019 11:53:08.471:INFO [karma-server]: Karma v3.1.3 server started at 
http://0.0.0.0:9876/
03 01 2019 11:53:08.471:INFO [launcher]: Launching browsers /software/applications/app/browserle 14% building modules 38/38 modules 0 active03 01 2019 11:53:08.491:INFO [launcher]: Starting browser /software/applications/app/browserless.sh
03 01 2019 11:53:08.491:DEBUG [temp-dir]: Creating temp dir at /tmp/karma-34604420
03 01 2019 11:53:08.543:DEBUG [launcher]: /software/applications/app/browserless.sh http://local 17% building modules 60/60 modules 0 active03 01 2019 11:53:10.906:DEBUG [launcher]: Process /software/applications/app/browserless.sh exited with code 0
03 01 2019 11:53:10.907:ERROR [launcher]: Cannot start /software/applications/app/browserless.sh
        + URL='http://localhost:9876/?id=34604420'
++ sed s/9876/3000/g
++ echo 'http://localhost:9876/?id=34604420'
+ DOCKER_URL='http://localhost:3000/?id=34604420'
+ trap 'killDockerContainer; exit 0' EXIT
+ echo 'Launching browserless: http://localhost:3000/?id=34604420'
+ docker run -d -p 3000:3000 --shm-size 2gb --name browserless --restart always -e DEBUG=browserless/chrome -e MAX_CONCURRENT_SESSIONS=10 domain:9082/browserless/chrome 'http://localhost:3000/?id=34604420'
+ killDockerContainer
+ echo 'Killing Docker Container'
+ docker rm -f browserless
+ exit 0

03 01 2019 11:53:10.907:ERROR [launcher]: /software/applications/app/browserless.sh stdout: Launching browserless: http://localhost:3000/?id=34604420
b7144002bc2c9abc786dbdd015a8426c9afcbd0713f408cf3103e980e2278649
Killing Docker Container
browserless

I managed to get this going via the karma-selenium-webdriver-launcher (instead of a custom browser) plugin and using selenium/standalone-chrome (instead of browserless) as the image.我设法通过 karma-selenium-webdriver-launcher(而不是自定义浏览器)插件并使用 selenium/standalone-chrome(而不是无浏览器)作为图像。 I'm sure this method would have worked with browserless just as well but the selenium gives me more options for future such as different browsers and using selenium grids etc.我确信这种方法也适用于无浏览器,但是 selenium 为我提供了更多的未来选择,例如不同的浏览器和使用 selenium 网格等。

Two important caveats are that the hostname that Karma uses must be changed in the config to the hostname of the vm karma is running on.两个重要的警告是 Karma 使用的主机名必须在配置中更改为正在运行的 vm karma 的主机名。 The other name you will be conserned about will be where docker is running.您将关注的另一个名称将是 docker 运行的位置。 This happens to be on localhost also so that will be used when specifying where karma expects the browser to be.这也恰好在 localhost 上,因此在指定 karma 期望浏览器所在的位置时将使用它。 Now knowing that much here was my karma config file to get this going.现在知道这里有很多是我的 karma 配置文件来实现这一点。

1 other important thing to consider will be the case where you want to run these tests on jenkins slaves thus hardcoding the hostname will not work.另一个需要考虑的重要事项是您想在 jenkins slaves 上运行这些测试,因此硬编码主机名将不起作用。 So you will have to find a way to get that address in your Karma config.因此,您必须找到一种方法来在 Karma 配置中获取该地址。

const webdriver = require('selenium-webdriver');
module.exports = function (config) {

  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-selenium-webdriver-launcher'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    hostname: 'server_hostname', // Here we need to change from default localhost to the hostname of the server
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true,
      reporters : ['coverage'],
      preprocessors : {'src/app/*.ts' : 'coverage'}
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: false,
    browsers: ['Chrome-wd'],
     customLaunchers: {
        'Chrome-wd': {
                base: 'SeleniumWebdriver',
                browserName: 'Chrome',
                getDriver: function() {
                return new webdriver.Builder()
                        .forBrowser('chrome')
                        .usingServer('http://hostname:4444/wd/hub') // Docker is run using docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
                        .build()
                }
        //      flags: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-software-rasterizer', '--disable-dev-shm-usage', '--remote-debugging-port=9222']
        }
    },
    singleRun: true
  });
};

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

相关问题 当Headless Chrome完成执行Karma单元测试时,Docker任务失败 - Docker task fails when Headless Chrome finishes executing Karma unit tests 无法使用 chrome-headless 在 docker 中运行 angular-cli karma-tests - Unable to run angular-cli karma-tests in docker with chrome-headless Karma测试在chrome中失败但是通过了phantomjs - Karma tests fail in chrome but pass in phantomjs 为业力测试 headlessChrome 构建 docker 图像 - Building docker image for karma testing headlessChrome 在 Docker 容器中使用 Chrome Headless 调试 Angular 测试 - Debug Angular Tests in with Chrome Headless in Docker Container Angular 5 - 在 Jasmine / Karma 测试中使用 HTTP 拦截器作为模拟 HTTP 请求 - Angular 5 - Using HTTP Interceptors as mock HTTP requests in Jasmine / Karma tests 使用 Karma 进行 Angular 单元测试,测试分支或 serviceSpy () 出错 - Angular Unit Tests using Karma, error with test branch or serviceSpy () 如何使用Karma在组件测试中存根Google gapi全局变量 - how to stub Google gapi global variable in component tests using Karma Karma没有执行任何测试 - Karma not executing any tests Karma 未在 Jenkins 上运行测试 - Karma not running tests on Jenkins
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM