【问题标题】:NodeJS: How to do nightmareJS e2e-testing on running docker applicationNodeJS:如何在运行的 docker 应用程序上进行 nightmareJS e2e-testing
【发布时间】:2017-05-10 20:00:21
【问题描述】:

我正在从我的生产构建应用程序(nodeJS 应用程序)创建一个 docker 映像/容器用于测试目的。 现在我想使用 mocha/chainightmareJS 进行一些 e2e 测试。所以我创建了一个非常基本的测试文件。

我现在的问题是如何测试正在运行的应用程序。所以我想像“加载”应用程序

- goto http://localhost
- check if login form is existing
- do login
- check if login was successful

我不知道如何在我的 docker image / e2e.js-file 中执行此操作...

这就是我创建 docker 映像的方式:

Dockerfile

# Use the production image as base image
FROM productive_build:latest

# Copy the test files
COPY e2e.js /

# Override the NODE_ENV environment variable to 'dev', in order to get required test packages
ENV NODE_ENV dev

# 1. Get test packages; AND
# 2. Install our test framework - mocha
RUN (cd programs/server && npm install)
RUN npm install -g mocha
RUN npm install chai nightmare

# Override the command, to run the test instead of the application
CMD ["mocha", "e2e.js", "--reporter", "spec"]

这就是我的基本 e2e.js 文件的样子:

e2e.js

var Nightmare = require('nightmare'),
    expect = require('chai').expect

describe('test', function () {
  it('should always be true', function () {
    var nightmare = Nightmare()
    nightmare.end()
    expect(true).to.be.true
  })
})

【问题讨论】:

    标签: javascript node.js testing docker nightmare


    【解决方案1】:

    看你的Dockerfile我不太确定,但看你的评论

    覆盖命令,运行测试而不是应用程序

    在安装了它的依赖项之后,你仍然需要自己启动网站,然后是 mocha。由于 docker 只打算一次运行一个进程,您可能需要查看supervisord,但您也可以将您的网站作为后台进程,然后启动您的测试。

    使用 bash 脚本的入口点而不是 CMD

    ENTRYPOINT ["./bin/run.sh"]
    

    脚本本身是这样的:

    #!/bin/bash
    npm start & mocha e2e.js --reporter spec
    

    至于测试本身,您可以执行类似的操作来测试您的登录流程。这只是一个示例,您可能需要调整一些东西,例如元素选择器和其他一些东西,但这仍然是一个很好的起点。

    var Nightmare = require('nightmare')
    var expect = require('chai').expect
    
    describe('test login', function () {
    
     it('should login and display my username', function (done) {
        var nightmare = Nightmare({ show: false })
    
        nightmare
          .goto('http://localhost:8000') // your local url with the correct port
          .type('#login', 'your login') // change value of the login input
          .type('#password', 'your password')
          .click('#submit') // click the submit button
          .wait('#my-username') // wait for this element to appear (page loaded)
          .evaluate(function () {
            // query something once logged on the page
            return document.querySelector('#my-username').innerText
          })
          .end()
          .then(function (result) {
            // the result will be the thing you grabbed from the window in the evaluate
            expect(result).to.equal('user3142695')
            done()
          })
          .catch(done)
    
      })
    
    })
    

    【讨论】:

    • 谢谢。我认为这正是我正在寻找的。还有一个问题:我应该把脚本放在哪里?我是否必须添加COPY run.sh /,然后使用ENTRYPOINT ["run.sh"]
    • 我如何知道用于 localhost 的正确端口?
    • 是的,你必须将脚本复制到容器中,如果你把它放在 / 你可能需要在命令中添加一个。至于端口,你只需要在本地开发时指定相同的url即可
    【解决方案2】:

    限定符:我不知道 nightmarejs 或您的所有依赖项

    您需要一个用于 node.js 的监控工具。 nodemon 是一个不错的选择,Docker 有一个关于使用 nodemonVisual Studio Code 实时调试 node.js 应用程序的教程。

    教程如下: https://github.com/docker/labs/blob/master/developer-tools/nodejs-debugging/VSCode-README.md

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2017-10-15
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多