【问题标题】:How to run e2e tests in teamcity, How to run the server in the background and run e2e如何在 teamcity 中运行 e2e 测试,如何在后台运行服务器并运行 e2e
【发布时间】:2016-10-27 13:31:28
【问题描述】:

如何在 teamcity 构建步骤中运行我需要的服务器以及 e2e 测试?

我的 Angular 2 应用程序有量角器 e2e 测试。 (我有一个有趣的 angular-cli 和 gulp 组合,但请耐心等待。)

这是我在本地运行测试的方式。我需要三个控制台窗口(w1、w2、w3)。

w1) 我需要做的第一件事是启动我的应用程序:

npm start -> 我在 package.json 中定义为 ng serve -prod

w2) 然后启动假后端,一个 express 网络服务器

npm run gulp e2e-server -> 我在我的包配置中定义了"gulp": "gulp",因为gulp 在teamcity 上不会被识别。

3w) 最后我可以运行我的 e2e 测试

npm run e2e -- e2e/protractor-teamcity.conf.js 我在我的包配置中定义了"pree2e": "webdriver-manager update""e2e": "protractor"

那么……

我需要手动停止我启动的两台服务器。

类似这样的 hack 会起作用:

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 是一种睡眠黑客,这也不理想。

有没有人找到在测试运行期间“在后台”运行命令然后将其杀死的解决方案?

【问题讨论】:

    标签: angularjs cmd protractor teamcity


    【解决方案1】:

    所以,这是一个可怕的 hack。那种暗示某些事情大错特错的hack,但哼哼:

    当 ng serve 运行时,它将控制台窗口标题更改为“angular-cli”,当 gulp 运行时将其更改为“gulp”(或“select gulp”)。我不希望这些标题会运行其他任何东西。 __kill-running-windows去杀掉这些窗口就够了。

    包.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 等服务留给读者想象):

    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:

    /**
     * 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);
    });
    

    【讨论】:

    • 这不是一直有效,我真希望我能想出另一种方法来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2018-05-10
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2019-12-25
    • 2020-11-16
    相关资源
    最近更新 更多