Javascript Cucumber.js场景中使用ZombieJS的回调问题

Javascript Cucumber.js场景中使用ZombieJS的回调问题,javascript,zombie.js,cucumberjs,Javascript,Zombie.js,Cucumberjs,我有一些使用BDD工具的经验,比如Cucumber和莴苣。我目前正在构建一个Phonegap应用程序,我想开始使用Cucumber.js为它创建验收测试。不幸的是,我有点问题 以下是我收集的基本功能文件: Feature: Authentication As a user I want to be able to log in and out Scenario: Logging in Given I am not logged in A

我有一些使用BDD工具的经验,比如Cucumber和莴苣。我目前正在构建一个Phonegap应用程序,我想开始使用Cucumber.js为它创建验收测试。不幸的是,我有点问题

以下是我收集的基本功能文件:

Feature: Authentication

    As a user
    I want to be able to log in and out

    Scenario: Logging in
        Given I am not logged in
        And I am on the page "login"
        When I fill in the "username" field with "student"
        And I fill in the "password" field with "password"
        And I click the "LOG IN" button
        Then I should see the text "STUDENT"
这是我的世界.js:

以下是我的步骤定义:

var wrapper = function () {
    "use strict";

    this.World = require("../support/world.js").World; // overwrite default World constructor

    this.Given(/^I am not logged in$/, function (callback) {
        // Clear local storage
        this.browser.localStorage("localhost:9001").clear();
        callback();
    });

    this.Given(/^I am on the page "([^"]*)"$/, function (page, callback) {
        // Visit page
        this.browser.visit('http://localhost:9001/app/index.html#' + page, callback);
    });
};

module.exports = wrapper;
我已经设置了一个Grunt任务,该任务首先在端口9001上运行connect服务器,然后运行Cucumber场景。第一步,但第二步失败

以下是我收到的错误消息:

Running "connect:cucumber" (connect) task
Started connect web server on http://localhost:9001

Running "cucumberjs:src" (cucumberjs) task
.Cannot call method 'add' of undefined TypeError: Cannot call method 'add' of undefined
    at <anonymous>:10:711
    at <anonymous>:10:874
    at <anonymous>:10:1224
    at Contextify.sandbox.run (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
    at DOMWindow.window._evaluate (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/window.js:188:25)
    at Object.HTML.languageProcessors.javascript (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:23:21)
    at define.proto._eval (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1480:47)
    at loaded (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:74:23)
    at /Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:76:20
    at Object.item.check (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:345:11)
FUUUU

(::) failed steps (::)

TypeError: Cannot call method 'add' of undefined
    at <anonymous>:10:711
    at <anonymous>:10:874
    at <anonymous>:10:1224
    at Contextify.sandbox.run (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
    at DOMWindow.window._evaluate (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/window.js:188:25)
    at Object.HTML.languageProcessors.javascript (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:23:21)
    at define.proto._eval (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1480:47)
    at loaded (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:74:23)
    at /Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:76:20
    at Object.item.check (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:345:11)

如果我插入回调;在第二步的主体之后,它通过。我不知道发生了什么事。为什么这个方案失败了?该应用程序本身可以正常工作。第二步的回调似乎从未触发。

如果将回调添加到第二步,则测试通过,因为刚刚跳过了visitPage

我的访问功能如下所示:

 this.visit = function(url, callback) {
   that.browser.visit(url, function(error) {
     if (error) {
       callback.fail(error);
     } else {
       callback.call(that, that.browser);
     }
   });
 });
但我认为真正的问题在您的页面上,因为sandbox.run是僵尸从页面开始执行自定义js代码的地方。所以这是一个匿名回调,在1224列的缩小脚本中?
也许你必须用console.log来追踪它。。。虽然zombie支持localStorage,但grep表示“添加自定义代码”

为什么要使用回调?他们混淆了你的代码。然而,等效的方法是使用异步/等待对,可以说,这将模拟java编码和正确的指令开始和结束:

var R = await visit () ;
await do_this_when_visit_is_done () ;
await do_that_when_do_this_is_done() ;

in cucumber :
this.Given(/^I am on the page "(.*)"$/, async function (page) 
{
  await this.page_is_loaded() ;
}

谢谢我的回答所涉及的范围并不比我们在这里讨论的问题稍大,这就是回调可能很难调试或读取,因此我想提到这个解决方案,它是一个更好的异步编码实现,这反过来可能有助于更轻松地调试当前问题。
var R = await visit () ;
await do_this_when_visit_is_done () ;
await do_that_when_do_this_is_done() ;

in cucumber :
this.Given(/^I am on the page "(.*)"$/, async function (page) 
{
  await this.page_is_loaded() ;
}