我正在从我的生产构建应用程序(nodeJS应用程序(创建一个docker映像/容器用于测试目的。现在我想使用 mocha/chai 和 nightmareJS 做一些 e2e 测试。所以我创建了一个非常基本的测试文件。
我现在的问题是如何测试正在运行的应用程序。所以我想像"加载"应用程序一样
- goto http://localhost
- check if login form is existing
- do login
- check if login was successful
我不知道如何在我的 docker 映像/e2e.js文件中执行此操作...
这是我创建 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
})
})
我不太确定查看您的Dockerfile
,但查看您的评论
重写命令,以运行测试而不是应用程序
在安装其依赖项后,您仍然需要启动网站本身,然后启动摩卡。由于 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)
})
})
限定符:我不知道噩梦或你所有的依赖项
您需要一个用于节点.js的监视工具。nodemon 是一个不错的选择,Docker 有一个关于使用 nodemon 和 Visual Studio Code 实时调试 node.js 应用程序的教程。
这是教程:https://github.com/docker/labs/blob/master/developer-tools/nodejs-debugging/VSCode-README.md