[英]Resolving multiple promises using Protractor, Cucumber, Chai as Promised
[英]Resolving promises in Protractor and Cucumber using Chai as Promised
最近,我和一位同事在使用Protractor和Chai作为Promised来实现黄瓜步骤定义的“正确”方法上存在一些分歧。 我们的争论源于相互之间缺乏对Cucumber环境中诺言解决方案的确切了解。
我们正在针对AngularJS应用进行测试,因此解决承诺和异步行为是必不可少的。 我们遇到的最大问题是强制同步测试行为,并使Cucumber等待步骤定义之间的承诺。 在某些情况下,我们已经观察到Cucumber似乎在Webdriver甚至没有执行它们之前就直接浏览步骤定义。 我们针对这个问题的解决方案各不相同...
考虑假设的情况:
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
大多数混乱源自“然后”步骤。 在此示例中,定义应断言搜索表单的所有字段都在页面上,这意味着要进行多次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);
});
但是,我的同事争辩说,为了满足承诺解决方案,您需要使用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;
});
});
});
});
后者对我来说真的很不对劲,但是我真的不知道前者是否正确。 我对finally()的理解是,它的工作方式与then()类似,因为它在继续执行之前要等待诺言解决。 我希望前面的示例按顺序等待每个Expect()调用,然后在最终的Expect()中通过notify()调用next(),以发出信号通知黄瓜继续进行下一步。
更令人困惑的是,我观察到其他同事这样写他们的期望:
expect(some_element).to.eventually.be.true.then(function() {
expect(some_other_element).to.eventually.be.true.then(function() {
expect(third_element).to.eventually.be.true.then(function() {
next();
});
});
});
所以我想提到的问题是:
提前谢谢了。
element(by.button('Run Search')).isPresent()
将不能解决直到两个element(by.model('optionsBox')).isPresent()
和element(by.model('searchTerms')).isPresent()
完成。 eventually
兑现诺言。 解释在这里: https : //stackoverflow.com/a/30790425/1432449 next()
放在then()
内没有什么不同 对我有用的是-如果存在html标记,则下面的函数将搜索始终等于true的内容。 我在每次测试结束时都调用此函数,并传入回调
function callbackWhenDone(callback) {
browser.wait(EC.presenceOf(element(by.css('html'))))
.then(function () {callback();})
}
这是一个简单测试中的用法:
this.Given(/^I go on "([^"]*)"$/, function (arg1, callback) {
browser.get('/' + arg1);
callbackWhenDone(callback);
});
我知道一点技巧,但是可以完成工作,并且在任何地方使用时看起来都非常干净
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.