在jenkins的bash脚本中运行casperjs test命令

时间:2018-07-25 12:31:43

标签: bash jenkins casperjs

为了使我的casperjs测试自动化,我从bash脚本运行它们,每个jenkins buold都调用该脚本。该脚本循环遍历我的casperjs测试文件(每个文件都适合一个场景),并使用基本的for循环运行它们,然后将输出存储到文本文件中:

for filename in *.js; do
    casperjs test "$filename" >> report.txt
    echo "********************************************" >> report.txt
done

我的casper测试文件具有以下结构:

/*global casper */
casper.test.begin("Test : participant connection", function (test) {
  "use strict";

  casper.start(myUrl);

  casper.then(function () {
    // some tests
  });

  casper.run(function () {
    test.done();
});

问题在于,有时测试的执行会在测试开始时甚至在测试内停止,而我不知道是什么原因造成的。

这里是一个例子:我尝试通过casperJS将自己连接到网络应用程序

casper.then(function () {
    this.echo("Current page is = " + this.getTitle());
    captureName = "moderatorLoginBeforeFilling";
    this.echo("----------------------------------------------------");
    util.takeCaptures(casper, captureName, captureFolder);
    this.wait(500, function () {
        this.echo("----------------------------------------------------");

        test.assertExist("form[name=\"leaderForm\"]");
        test.assertExist("input[name=\"leaderEmail\"]");
        test.assertExist("input[name=\"leaderPwd\"]");
        this.fill("form[name=\"leaderForm\"]", {
            "leaderEmail": leaderEmail,
            "leaderPwd": leaderPwd
        }, false);

        captureName = "moderatorLoginAfterFilling";
        this.echo("----------------------------------------------------");
        util.takeCaptures(casper, captureName, captureFolder);
        this.wait(500, function () {
            this.echo("----------------------------------------------------");

            test.assertExist("button[type=\"submit\"]");
            this.click("button[type=\"submit\"]");

            this.waitForUrl(Config.app.baseUrl + "moderator#/", function then() {
                this.echo("You're well arrived on the moderator dashboard page.");
            }, function timeout() {
                this.echo("Issue while evaluating your presence on the moderator dashboard page.").exit();
            });
        });
    });
});

// Connected, screenshots of dashboard
casper.then(function () {
    this.echo("Current page is = " + this.getTitle());
    captureName = "dashboard";
    this.echo("----------------------------------------------------");
    util.takeCaptures(casper, captureName, captureFolder);
    this.wait(500, function () {
        this.echo("----------------------------------------------------");
    });
});

在测试运行期间,它在获取最后的屏幕截图(此处是我的report.txt内容)之前被阻止:

Current page is = Moderator Login
----------------------------------------------------
Screenshot for mobile-portrait (320x565) - moderatorLoginBeforeFilling
Screenshot for mobile-landscape (565x320) - moderatorLoginBeforeFilling
Screenshot for iPad-portrait (768x1024) - moderatorLoginBeforeFilling
Screenshot for iPad-landscape (1024x768) - moderatorLoginBeforeFilling
Screenshot for desktop-standard (1920x1080) - moderatorLoginBeforeFilling
----------------------------------------------------
[32;1mPASS[0m Find an element matching: form[name="leaderForm"]
[32;1mPASS[0m Find an element matching: input[name="leaderEmail"]
[32;1mPASS[0m Find an element matching: input[name="leaderPwd"]
----------------------------------------------------
Screenshot for mobile-portrait (320x565) - moderatorLoginAfterFilling
Screenshot for mobile-landscape (565x320) - moderatorLoginAfterFilling
Screenshot for iPad-portrait (768x1024) - moderatorLoginAfterFilling
Screenshot for iPad-landscape (1024x768) - moderatorLoginAfterFilling
Screenshot for desktop-standard (1920x1080) - moderatorLoginAfterFilling
----------------------------------------------------
[32;1mPASS[0m Find an element matching: button[type="submit"]
You're well arrived on the moderator dashboard page.
Current page is = Moderator Login
----------------------------------------------------

在我决定手动终止该进程之前,它一直处于阻塞状态。 有一会儿,我认为这是一个问题,原因是在循环中执行了casperjs命令,因此我尝试使用wait,但并没有解决我的问题。

可能是什么问题? 谢谢您的帮助

0 个答案:

没有答案