通过Jenkins CI在Docker容器中运行Selenium测试的最简单方法 [英] Easiest way to run Selenium tests in a Docker container over Jenkins CI

查看:201
本文介绍了通过Jenkins CI在Docker容器中运行Selenium测试的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行我的自动化测试,该测试是通过Docker容器中的Jenkins CI用Nightwatch-Cucumber编写的.我有一个要用于的Docker映像.

I want to execute my automated tests, written in Nightwatch-Cucumber over a Jenkins CI in a Docker container. I have a Docker image that I want to use for it.

这是我想做的更详细的事情.

This is what I want to do in more detail.

  1. 开始对Jenkins CI工作进行测试
  2. 在同一台机器上加载Docker映像,并且相关的Docker容器将启动.该容器基于Unix OS.另外,将在Docker容器中执行一些配置.
  3. 将通过xvfb以无头模式执行(本地或远程)测试,并且报告将保存在Jenkins机器上.

在GitLab CI上,我已经通过.gitlab-ci.yml配置文件实现了它,并且运行非常好:

Over GitLab CI I've realized it over a .gitlab-ci.yml config file and it runs very good:

image: "my-docker-image"

stages:
  - "chrome-tests"

before_script:
  - "apt-get update"
  - "apt-get install -y wget bzip2"
  - "npm install"

cache:
  paths:
    - node_modules/
run-tests-on-chrome:
  stage: "chrome-tests"
  script:
    - "whereis xvfb-run"
    - "xvfb-run --server-args='-screen 0 1600x1200x24' npm run test-chrome"

但是我想用Jenkins CI实现相同的过程.什么是最简单的方法,然后在由詹金斯(Jenkins)调用的Docker镜像中运行我的自动化测试?我是否应该编写Dockerfile或或或或?

But I want to realize the same procedure with Jenkins CI. What is the easiest way to do it and ro run my automated tests in a Docker image which is called by Jenkins? Should I write a Dockerfile or not or or or?

推荐答案

我目前正在运行用PHP编写的Selenium Test脚本,并使用Docker Compose通过Jenkins运行它们.您也可以做同样的事情,而不必麻烦自己处理Xvfb.

I'm currently running Selenium Test scripts written in PHP and running them through Jenkins using Docker Compose. You can do the same as well without the hassle of dealing with Xvfb yourself.

要使用docker容器内的无头浏览器运行Selenium测试并将其通过docker-compose链接到您的应用程序,您可以简单地使用预定义的独立服务器.

To run your Selenium tests using headless browsers inside a docker container and linking it to your application with docker-compose, you can simply use the pre-defined standalone server.

https://github.com/SeleniumHQ/docker-selenium

我目前正在使用Chrome独立映像.

I'm currently using the Chrome Standalone image.

这是您的docker-compose应该是这样的:

Here's what your docker-compose should look like:

version: '3'
services:
  your-app:
    build:
      context: .
      dockerfile: Dockerfile
  your_selenium_application: 
    build:
      context: .
      dockerfile: Dockerfile.selenium.test
    depends_on:
      - chrome-server
      - your-app
  chrome-server:
    image: selenium/standalone-chrome:3.4.0-einsteinium

运行docker-compose时,它将启动您的应用程序,将与您的应用程序交互的硒环境以及将为您提供无头浏览器的独立服务器.因为它们是链接的,所以在您的硒代码内部,您可以通过例如your-app:80向主机发出测试请求.您的无头浏览器将是chrome-server:4444/wd/hub,这是默认地址.

When running docker-compose, it will spin up your application, the selenium environment that will be interacting with your app, and the standalone server that will provide you with your headless browser. Because they are linked, inside your selenium code, you can make your test requests to the host via your-app:80 for example. Your headless browser will be chrome-server:4444/wd/hub which is the default address.

这一切都可以在Jenkins内部完成,仅需在Jenkins Job内的Execute Shell中使用一个命令即可. docker-compose还可以让您轻松地在本地计算机上运行测试,并且结果应该相同.

This can all be done inside of Jenkins using only one command in your Execute Shell inside of your Jenkins Job. docker-compose will also allow you to easily run the tests on your local machine as well, and the results should be identical.

这篇关于通过Jenkins CI在Docker容器中运行Selenium测试的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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