![](/img/trans.png)
[英]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.