【问题标题】:e2e testing of VueJS app in gitlab-ci在 gitlab-ci 中对 VueJS 应用程序进行 e2e 测试
【发布时间】:2018-05-22 07:55:03
【问题描述】:

我使用 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 中运行 e2e 测试?

【问题讨论】:

  • 运行这些测试的机器需要安装 JAVA。 Selenium 没有它就无法运行。
  • 所以您认为只需在 selenium/standalone-chrome Docker 映像之上添加 java 就可以解决问题吗?还是我需要在这里做一些更详细的事情
  • 不知何故我错过了您为此使用 Docker。该图像应该已经安装了 Java。现在我真的很困惑出了什么问题。我会确保您使用的 Gitlab 运行器具有 Java 功能。您正在使用的 docker 映像已经安装了 Java,所以不可能是这样。
  • 似乎没有安装Java,我在CI脚本中添加了一个显示Java版本的命令,它返回$ java -v /bin/bash: line 63: java: command not found
  • 遗憾的是我还没有解决它,@tehbeardedone 的回答帮助我取得了进展,但不足以让测试运行。我会尝试一些想法,我会及时通知你

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


【解决方案1】:

好的,现在我查看了您的配置和撰写文件,我想我看到了您的问题。您需要做的第一件事是在 .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
          }
        }
      }
    }
  }
}

【讨论】:

  • 你的答案我已经取得了相当大的进步,现在开始测试。但是,现在我收到错误Timed out while waiting for element <#app> to be present for 5000 milliseconds. - expected "visible" but got: "not found"。我会调查,但如果您有任何想法,请随时发表评论
  • 是的,您只需将外部全局文件中 waitForConditionTimeout 的超时时间增加到您的应用程序可以接受的值。 github.com/nightwatchjs/nightwatch/blob/…
  • 即使超时 15 秒也会失败,所以我猜问题在于没有启动开发服务器
  • 你有没有得到这个工作?当我输出页面的源代码时,它显示“无法访问此站点”和“localhost 拒绝连接”。走到这一步之后,在我看来,Selenium 服务看不到 devServer。在其他地方我读到“因为 Selenium 服务器 [is] 在单独的服务容器中运行,您将无法从主应用程序容器访问 localhost 端点。这意味着您的应用程序需要可以从 Internet 访问才能访问以这种方式进行测试。”
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2021-09-28
  • 2015-10-24
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
相关资源
最近更新 更多