vue.js - 在 gitlab-ci 中对 VueJS 应用程序进行端到端测试

标签 vue.js vuejs2 gitlab-ci nightwatch.js

我已经使用 vue-cli 生成了一个 VueJS 项目,包括使用 Nightswatch.js 的端到端测试。

我正在使用以下 .gitlab-ci.yml 文件

services:
  - selenium/standalone-chrome

stages:
  - test
  - pages

test:
  image: node:6.11
  stage: test
  before_script:
    - node -v
    - npm -v
  script:
    - npm install
    - npm test

pages:
  image: node:6.11
  stage: pages
  before_script:
    - node -v
    - npm -v
  script:
    - npm install
    - npm run build
    - cp -R ./dist ./public
    - cd ./public
    - ls
  artifacts:
    paths:
      - public
  only:
    - master

这是nightswatch.conf.js 文件

require('babel-register')
var config = require('../../config')

// http://nightwatchjs.org/gettingstarted#settings-file
module.exports = {
  src_folders: ['test/e2e/specs'],
  output_folder: 'test/e2e/reports',
  custom_assertions_path: ['test/e2e/custom-assertions'],

  selenium: {
    start_process: true,
    server_path: require('selenium-server').path,
    host: '127.0.0.1',
    port: 4444,
    cli_args: {
      'webdriver.chrome.driver': require('chromedriver').path
    }
  },

  test_settings: {
    default: {
      selenium_port: 4444,
      selenium_host: 'localhost',
      silent: true,
      globals: {
        devServerURL: 'http://localhost:' + (process.env.PORT || config.dev.port)
      }
    },

    chrome: {
      desiredCapabilities: {
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true
      }
    },

    firefox: {
      desiredCapabilities: {
        browserName: 'firefox',
        javascriptEnabled: true,
        acceptSslCerts: true
      }
    }
  }
}

在 Gitlab-CI 中,作业通过了,但查看日志时只有单元测试通过,而不是端到端测试。

> node test/e2e/runner.js

Starting selenium server... 
An error occurred while trying to start Selenium. Check if JAVA is installed on your machine.
{ Error: spawn java ENOENT
    at exports._errnoException (util.js:1020:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:197:32)
    at onErrorNT (internal/child_process.js:376:16)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:383:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:496:3
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn java',
  path: 'java',
  spawnargs: 
   [ '-Dwebdriver.chrome.driver=/builds/Overdrivr/frontend/node_modules/chromedriver/lib/chromedriver/chromedriver',
     '-jar',
     '/builds/Overdrivr/frontend/node_modules/selenium-server/lib/runner/selenium-server-standalone-3.8.1.jar',
     '-port',
     4444 ] }
INFO Selenium process finished.
Job succeeded

如何正确配置 gitlab-ci 或 nightswatch 以在 Gitlab-CI 中运行端到端测试?

最佳答案

好的,现在我进一步查看了您的配置和撰写文件,我想我明白了您的问题。您需要做的第一件事是在 .yml 文件中为您的 selenium/standalone-chrome 服务命名。问题是您正在尝试在未安装 java 的测试容器(节点镜像)中独立启动 selenium。但是,selenium/standalone-chrome 图像可以,这是您要指向测试而不是 localhost

的地方
services:
  "chrome"
  - selenium/standalone-chrome

#...rest of file can stay the same

您需要做的第二件事是从您的 nightwatch 配置中完全删除 selenium 部分,并在您的 chrome 服务的 test_settings 下指向 selenium_host

selenium_host: 'chrome',

这是为我工作的 nightwatch.jsondocker-compose.yml

docker-compose.yml

version: '3'
services:  
chrome:
  image: selenium/standalone-chrome
tests:
  image: nightwatch-tests
  environment: 
    - ENV_PASS=${ENV_PASS}
  depends_on:
    - chrome

nightwatch.json

{
  "src_folders": [
    "nw_tests"
  ],
  "output_folder": "nw_reports",
  "page_objects_path": "./nw_tests/pages",
  "globals_path": "globals.js",
  "test_workers": false,  
  "test_settings": {
    "default": {
      "launchUrl": "https://mylaunchurl/login",
      "selenium_port": 4444,
      "selenium_host": "chrome",
      "silent": true,
      "screenshots": {
        "enabled": true,
        "path": "nw_screenshots"
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions" : {
          "args": ["deny-permission-prompts"],
          "prefs": {
            "profile.default_content_settings.popups": 0,
            "download.prompt_for_download": false
          }
        }
      }
    }
  }
}

关于vue.js - 在 gitlab-ci 中对 VueJS 应用程序进行端到端测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47708097/

相关文章:

javascript - 为什么组件不响应自定义事件?

javascript - 如何在加载/显示页面之前使用 Vue(路由器)获取 GET 请求变量

mysql - Gitlab CI + DinD + MySQL 服务权限问题

docker - GitLab CI : how to connect to the docker container started in . gitlab-ci.yml 脚本?

javascript - v-绑定(bind) :checked not working in all component instances

vue.js - 切换页面时vue router挂载两次

vue.js - 使用 vee-validate 验证单选按钮交换的选择列表不显示验证消息

unit-testing - Vue-测试工具 | Jest : How to handle dependencies?

amazon-web-services - 如何使用 AWS CLI 更新弹性 beantalk 代码?

javascript - vue.js VeeValidate - 自定义验证器正确编译错误但不切换错误类