如何在teamcity中运行e2e测试,如何在后台运行服务器并运行e2e [英] How to run e2e tests in teamcity, How to run the server in the background and run e2e
问题描述
如何在teamcity构建步骤中运行我需要的服务器以及e2e测试?
我对我的angular 2应用程序进行了量角器e2e测试. (我混合了angular-cli和gulp,但我要忍受.)
I have protractor e2e test for my angular 2 application. (I have a funny mix of angular-cli and gulp, but bear with me.)
这是我在本地运行测试的方式.我需要三个控制台窗口(w1,w2,w3).
Here's how I run my tests locally. I need three console windows (w1,w2,w3).
w1)我要做的第一件事是启动我的应用程序:
w1) First thing I need to do is start my application:
npm start
->我在package.json中定义为ng serve -prod
w2)然后启动伪造的后端,一个快速的网络服务器
w2) Then start the fake back-end, an express webserver
npm run gulp e2e-server
->我在程序包配置中定义了"gulp": "gulp"
,因为gulp
在teamcity上不会被识别.
npm run gulp e2e-server
-> I've defined "gulp": "gulp"
in my package config, because gulp
won't be recognised on teamcity.
3w)然后我终于可以运行我的e2e测试
3w) And then finally I can run my e2e tests
npm run e2e -- e2e/protractor-teamcity.conf.js
我已经在程序包配置中定义了"pree2e": "webdriver-manager update"
和"e2e": "protractor"
然后...
我需要手动停止我启动的两台服务器.
I need to manually stop the two servers I started.
类似这种hack的东西会起作用:
Something like this hack will work:
npm run gulp e2e-clean && start "MyWindow" cmd /c "start npm start && npm run gulp e2e-server" && ping -n 31 127.0.0.1 >nul && npm run e2e -- e2e/protractor-teamcity.conf.js
但是start
创建的控制台窗口将永远不会停止.我不确定这样做的后果(我怀疑这将成功运行两次). ping是一个睡眠hack,也不理想.
But start
creates console windows that will never stop. I'm not sure what the consequences of this are (I doubt this will run successfully twice). The ping is a sleep hack, which isn't ideal either.
是否有人找到了一种解决方案,可以在测试运行期间在后台"运行命令,然后将该命令杀死?
推荐答案
所以,这是一个可怕的骇客.暗示某些事情的骇客是完全错误的,但是哼哼:
So, this is a terrible hack. The sort of hack that suggests something is deeply wrong, but ho-hum:
当ng serve运行时,它将控制台窗口标题更改为"angular-cli",当gulp运行时将其更改为"gulp"(或"select gulp").我预计这些标题不会再有其他功能. 这足以写__kill-running-windows
去杀死这些窗口.
When ng serve runs it will change the console window title to "angular-cli", when gulp runs it which change it to "gulp" (or "select gulp"). I don't expect anything else will be running with these titles. This is enough to write __kill-running-windows
to go and kill these windows.
Package.json:
Package.json:
"scripts": {
"start": "ng serve -prod",
"test": "gulp test-teamcity",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"gulp": "gulp",
"e2e-teamcity": "gulp _e2e-clean && npm run _e2e-teamcity & npm run _kill-running-windows",
"_e2e-teamcity": "npm run _e2e-servers && gulp __wait-60 && gulp _e2e-test-teamcity",
"_e2e-servers": "start gulp _e2e-server && start gulp serve",
"_kill-running-windows": "taskkill /fi \"Windowtitle eq gulp\" & taskkill /fi \"Windowtitle eq select gulp\" & taskkill /fi \"Windowtitle eq angular-cli\" & taskkill /fi \"Windowtitle eq select angular-cli\""
},
代码(无论如何,有趣的部分,我都会把诸如gulp的内容留给读者想象):
The code (the interesting parts anyway, I'll leave what eg gulp serve to the readers imagination):
var expressServer = require("gulp-express");
var process = require("child_process");
var shell = require("gulp-shell");
/**
* Run vanilla e2e tests with teamcity reporter
*
* (Remember to call `e2e-server`, `serve` and edit `config.json` to point at config.e2e.json` first)
*/
gulp.task("_e2e-test-teamcity", function(done) {
return gulp.src("")
.pipe(
shell(["npm run e2e -- e2e/protractor-teamcity.conf.js"])
)
});
gulp.task("__wait-60", function(done) {
// HACK: Do 61 pings -> wait 30 seconds
process.exec("ping 127.0.0.1 -n 61 > nul", function (err, stdout, stderr) {
done();
});
});
/**
* Run mock backend for the e2e, with canned answers to all calls
* !! Use config.e2e.json in your application in order to point at this !!
*/
gulp.task("_e2e-server", function () {
expressServer.run(["./e2e/server.js"]);
gulp.watch(["./e2e/server.js"], expressServer.notify);
});
由于某种原因,将更多代码移到gulp中似乎使构建无法在teamcity上完成.但是,这是我在本地使用的e2e,更多基于gulp:
For some reason, moving more code into gulp seemed to make the builds never finish on teamcity. But here's the e2e I use locally, which is more gulp based:
/**
* Run vanilla e2e tests
* Cleans screenshots folder, tarts the application, starts the mock server
* Leaves command windows running the servers open at the end of the test run
*
* 30 second wait for tests to start
*
* (Remember to edit `config.json` to point at config.e2e.json` first)
*/
gulp.task("e2e", ["_e2e-clean"], function (done) {
gulp.src("")
.pipe(
shell(["start gulp _e2e-server"])
).pipe(
shell(["start gulp serve"])
);
runSequence("__wait-30","_e2e-test", done);
});
这篇关于如何在teamcity中运行e2e测试,如何在后台运行服务器并运行e2e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!