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

问题描述 投票:1回答:1

对于CI服务器上的测试,我想使用Chrome的图像而不是PhantomJS。我可以使用和不使用puppeteer来执行此操作,但两者都要求我在服务器上安装chrome-stable包。因此,我想要一个更轻量级的方法,因此使用docker镜像。

对于Karma,根据文档,如果我想使用自定义浏览器,我必须为浏览器指定一个脚本。此外,Karma会将1个参数传递给此脚本,即url。对于这个要求,我还拉了无浏览器码头图像browserless quickstart

我已经创建了脚本并可以调整Karma测试,但看起来业力在脚本执行完毕之前进入失败状态。关于我在哪里出错的任何想法。容器按预期运行,curl命令到localhost:3000返回预期的内容。

自定义脚本

#!/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 

测试运行的输出

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
angular karma-jasmine karma-runner puppeteer
1个回答
1
投票

我设法通过karma-selenium-webdriver-launcher(而不是自定义浏览器)插件并使用selenium / standalone-chrome(而不是无浏览器)作为图像。我确信这种方法也适用于无浏览器,但是selenium为我提供了更多选择,例如不同的浏览器和使用selenium网格等。

两个重要的警告是,Karma使用的主机名必须在配置中更改为正在运行的vm karma的主机名。您将被关注的另一个名称将是docker正在运行的位置。这恰好也在localhost上,因此在指定karma期望浏览器的位置时将使用它。现在知道这里有很多我的业力配置文件来实现这一目标。

另外要考虑的另一个重要事项是你想在jenkins从站上运行这些测试的情况,因此硬编码主机名将无效。所以你必须找到一种方法来在你的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
  });
};
© www.soinside.com 2019 - 2024. All rights reserved.