使用Chrome的Docker映像进行业力测试 [英] Karma tests using a Docker image of Chrome

查看:100
本文介绍了使用Chrome的Docker映像进行业力测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于CI服务器上的测试,我想使用Chrome的图像而不是PhantomJS. 我可以在有和没有puppeteer的情况下执行此操作,但是两者都需要我在服务器上安装chrome-stable软件包.因此,我想使用一种更轻量的方法,从而使用docker映像.

For tests on CI server I want to use an image of Chrome instead of PhantomJS. I can do this with and without puppeteer but both require me to install chrome-stable package on the server. Thus I want a more lightweight method hence the use of the docker image.

对于Karma,根据文档,如果要使用自定义浏览器,则必须为浏览器指定脚本. Karma还将向此脚本传递1个参数,即url.为此,我还拉出了无浏览器的Docker映像无浏览器的快速入门.

For Karma, according to the docs, I must specify a script for the browser if i want to use a custom browser. Also Karma will pass 1 argument to this script, the url. For this requirement I also pulled the browserless docker image browserless quickstart.

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

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. Any ideas on where I am going wrong here. The container runs as expected and curl command to localhost:3000 returns what is expected.

自定义脚本

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

推荐答案

我设法通过karma-selenium-webdriver-launcher(而不是自定义浏览器)插件并使用selenium/standalone-chrome(而不是无浏览器)作为图片.我敢肯定,这种方法在没有浏览器的情况下也可以使用,但是硒为我提供了更多的选择,例如将来使用不同的浏览器以及使用硒网格等.

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. 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.

两个重要的警告是,必须在配置中将Karma使用的主机名更改为运行vm karma的主机名.您会想到的另一个名称将是docker运行的位置.这也恰好在本地主机上,因此将在指定业力期望浏览器所在的位置时使用.现在知道了很多,这就是我的业力配置文件.

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. The other name you will be conserned about will be where docker is running. This happens to be on localhost also so that will be used when specifying where karma expects the browser to be. Now knowing that much here was my karma config file to get this going.

要考虑的另一件重要事情是,您想在jenkins从属服务器上运行这些测试,因此对主机名进行硬编码将不起作用.因此,您将必须找到一种在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. So you will have to find a way to get that address in your Karma config.

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
  });
};

这篇关于使用Chrome的Docker映像进行业力测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆