3

我正在尝试编写一个非常简单的 CasperJS 脚本来测试网站上的登录按钮。点击事件后,网站顶部会打开一个模式窗口,您可以填写登录字段。

使用该网站,这工作得很好。使用casperjs test my-script-file --engine=slimerjs也非常好。没有 slimerjs,代码会失败。

按钮的定义方式:

<button class="btn btn-strong-submit" data-action="join">Join</button>

我的测试:

casper.test.begin('testing', 3, function suite(test)
{
    casper.start();
    casper.thenOpen("http://the-website-to-test-url", function()
    {
        casper.wait(5000, function(){
            casper.thenClick("button.btn.btn-strong-submit");
        });
    });

    casper.then(function () {
        casper.wait(3000);
        casper.capture("screen.png");
    });

    casper.then(function(){
        casper.assertVisible("div.join_container");
    });

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

在网站上,单击按钮将转到http://the-website-to-test-url.com/#join,因为该网站的工作方式是等待单击并捕获使用 jQuery,然后找到 [ data-action],形成#[data-action],然后以这种方式构建 URL。

但是 CasperJS 似乎不理解一个点击,然后在后台从 jQuery 中捕获。有任何想法吗?

终端输出更新:

[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: http://www.MYURL.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://www.MYURL.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.MYURL.com/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 http://www.MYURL.com/ (HTTP 200)
[info] [phantom] Step anonymous 2/4: done in 721ms.
[info] [phantom] Step anonymous 3/4 http://www.MYURL.com/ (HTTP 200)
[info] [phantom] Step anonymous 3/4: done in 731ms.
[info] [phantom] Step _step 4/5 http://www.MYURL.com/ (HTTP 200)
[info] [phantom] Step _step 4/5: done in 752ms.
[info] [phantom] wait() finished waiting for 5000ms.
[debug] [phantom] Mouse event 'mousedown' on selector: .btn.btn-strong-submit
[debug] [phantom] Mouse event 'mouseup' on selector: .btn.btn-strong-submit
[debug] [phantom] Mouse event 'click' on selector: .btn.btn-strong-submit
[info] [phantom] Step anonymous 5/5 http://www.MYURL.com/ (HTTP 200) 

//After click URL should have changed to http://www.MYURL.com/#login

[info] [phantom] Step anonymous 5/5: done in 5772ms.
[info] [phantom] Step _step 6/6 http://www.MYURL/ (HTTP 200)
[info] [phantom] Step _step 6/6: done in 5792ms.
[info] [phantom] wait() finished waiting for 3000ms.
[debug] [phantom] Capturing page to /home/TEMP/screen.png
[info] [phantom] Capture saved to /home/TEMP/screen.png
[info] [phantom] Done 6 steps in 9367ms
4

1 回答 1

1

Expanding on Artjom's comment, I think you should change this:

casper.then(function () {
    casper.wait(3000);
    casper.capture("screen.png");
});

to be:

casper.wait(3000, function () {
    casper.capture("screen.png");
});

This is a bit more descriptive, not just more compact: "Wait 3000ms then do ...".

(Aside: casper.then( doSomething ) and casper.wait(0, doSomething) are the same thing.)

NOTE: the downside of this approach is it adds a 3-second delay to your unit test. You could rewrite it as:

casper.waitUntilVisible('div.join_container', {
    casper.capture("screen.png");
});

Then as soon as it is visible it will take the screenshot, then progress to the next step. The downside of this way is that your assert always works; if there is a problem you get a time-out instead of a test failure. (That may or may not matter, it depends on what you are testing and why.)

于 2015-02-03T09:06:44.163 回答