简体   繁体   English

Zombie / Mocha browser.wait(…)方法未执行。

[英]Zombie/Mocha browser.wait(…) method doesn't execute.

Let's get straight to the point. 让我们直接说清楚。

Here is a piece of code I'm using which should test the web app (which at the moment only shows <p>Welcome!</p> ) 这是我正在使用的一段代码,可以测试该Web应用程序(目前仅显示<p>Welcome!</p>

var app = require('../app');
var assert = require('assert');
var Browser = require('zombie');

describe('home page', function() {
    var browser, server;

    before(function() {
        server = app.listen(3000);
        browser = new Browser({site: 'http://localhost:3000', debug: true});
    });

    it('should show welcome', function(done) {

    // Wait until page is loaded
        function pageLoaded(window) {
            console.log('4');
            return window.document.querySelector(".container");
        }
        console.log('1');
        browser.visit("/");
        console.log('2');
        browser.wait(pageLoaded, function() {
            console.log('3');
            assert.ok(browser.success);
            assert.equal(browser.text('p'), 'Welcomexxx!');
            console.log(browser.html());
        });
        done();
    });

    after(function(done) {
        server.close(done);
    }); 
});

This test should not pass as I'm checking for Welcomexxx! 由于我正在检查Welcomexxx,因此该测试不应通过! not Welcome!. 不欢迎!。 However it always passes regardless of what I test for. 但是,无论我测试什么,它总是会通过。 As You can see I added some console outputs to the code to see whats happening. 如您所见,我在代码中添加了一些控制台输出,以查看发生了什么。 To my surprise neither 3 nor 4 shows in the console. 令我惊讶的是,控制台中没有显示3或4。 This basically means that the wait function does not execute. 这基本上意味着等待功能不会执行。

Here is the expected output for a non-failed test: 这是非失败测试的预期输出:

home page
✓ should show welcome
GET / 200 5ms - 15b
1 passing (63ms)

And this is my output for the above test: 这是上述测试的输出:

home page
1
Zombie: Opened window http://localhost:3000/  //zombie debug line
2
✓ should show welcome 
1 passing (22ms)

As You can see there is also no "get" statement in the output. 如您所见,输出中也没有“ get”语句。 Also zombie seems to work in general, as browser.visit shows console output indicating that something happened. 僵尸似乎也可以正常工作,因为browser.visit会显示控制台输出,表明发生了某些事情。

Can anyone shed some light on why this happens? 谁能阐明为什么会这样? I'm capable of running the app with foreman start or node app and I can see in my browser that everything is working fine (in theory). 我能够使用领班启动或节点应用程序运行该应用程序,并且在浏览器中可以看到一切正常(理论上)。

Seems to me that the call to done() should be in the callback handler to browser.wait() 在我看来,对done()的调用应该在browser.wait()的回调处理程序中

it('should show welcome', function(done){
  ....
  browser.wait(pageLoaded, function(){
     ...
     assert( ... );
     done();
  });
});

You has to deal with zombie with callback function or in promise like below example: 您必须使用回调函数或promise处理僵尸,如以下示例所示:

browser.visit("/").then(function(){
    assert.ok(browser.success);
    assert.equal(browser.text('p'), 'Welcomexxx!');
    console.log(browser.html());
}).then(done, done);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM