我正在从我的高效构建应用程序(nodeJS app)创建一个用于测试目的的docker镜像/容器。 现在我想使用 mocha / chai 和 nightmareJS 进行一些e2e测试。所以我创建了一个非常基本的测试文件。
我现在的问题是如何测试正在运行的应用程序。所以我想“加载”应用程序,如
- goto http://localhost
- check if login form is existing
- do login
- check if login was successful
我不知道如何在我的docker image / e2e.js文件中执行此操作...
这就是我创建泊坞窗图像的方式:
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
})
})
答案 0 :(得分: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)
})
})
答案 1 :(得分:0)
限定符:我不知道 nightmarejs 或所有依赖项
您需要 node.js 的监控工具。 nodemon 是一个不错的选择,Docker有一个关于使用 nodemon 和 Visual Studio Code 来实时调试node.js应用程序的教程。
这是教程: https://github.com/docker/labs/blob/master/developer-tools/nodejs-debugging/VSCode-README.md