小编And*_*hum的帖子

使用Chai作为承诺解决量角器和黄瓜的承诺

最近一位同事和我对使用Protractor和Chai作为承诺实施Cucumber步骤定义的"正确"方法存在一些分歧.我们的论点来自于对Cucumber背景下的承诺解决方案的准确性的完全缺乏了解.

我们正在测试AngularJS应用程序,因此解决promise和异步行为是必要的恶魔.我们遇到的最大问题是强制同步测试行为并让Cucumber等待步骤定义之间的承诺.在某些情况下,我们观察到的情况是,在Webdriver甚至执行它们之前,Cucumber似乎直接通过步骤定义.我们对这个问题的解决方案各不

考虑假设情景:

Scenario: When a user logs in, they should see search form
  Given a user exists in the system
  When the user logs into the application
  Then the search form should be displayed
Run Code Online (Sandbox Code Playgroud)

大多数混淆起源于Then步骤.在此示例中,定义应声明页面上存在搜索表单的所有字段,这意味着多个isPresent()检查.

从我能够找到的文档和示例中,我觉得断言看起来应该是这样的:

this.Then(/the search form should be displayed/, function(next) {
    expect(element(by.model('searchTerms')).isPresent()).to.eventually.be.true;
    expect(element(by.model('optionsBox')).isPresent()).to.eventually.be.true;
    expect(element(by.button('Run Search')).isPresent()).to.eventually.be.true.and.notify(next);
});
Run Code Online (Sandbox Code Playgroud)

但是,我的同事认为,为了满足承诺解决方案,您需要使用then()链接您的期望,如下所示:

this.Then(/the search form should be displayed/, function(next) {
    element(by.model('searchTerms')).isPresent().then(function(result) {
        expect(result).to.be.true;

    }).then(function() {
        element(by.model('optionsBox')).isPresent().then(function(result) {
            expect(result).to.be.true;

        }).then(function() {
            element(by.button('Run Search')).isPresent().then(function(result) {
                expect(result).to.be.true;
                next;
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

后者对我来说感觉真的不对,但我不知道前者是否正确.我最终理解的方式()是它与then()的工作方式类似,因为它在继续之前等待承诺解决.我希望前面的例子按顺序等待每个expect()调用,然后通过最终expect()中的notify()调用next()来发信号给黄瓜继续下一步.

为了增加更多的困惑,我观察到其他同事写下这样的期望: …

javascript angularjs chai protractor cucumberjs

6
推荐指数
1
解决办法
4936
查看次数

标签 统计

angularjs ×1

chai ×1

cucumberjs ×1

javascript ×1

protractor ×1