簡體   English   中英

使用Chai作為承諾來實現量角器和黃瓜中的承諾

[英]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();
        });
    });
});

所以我想提到的問題是:

  • 以上任何一種還算對嗎?
  • 最終()實際上是做什么的? 是否會強制執行then()之類的同步行為?
  • and.notify(next)真正做什么? 與在then()內部調用next()有什么不同?
  • 有沒有我們尚未找到的最佳實踐指南,可以使其中的任何內容更加清晰?

提前謝謝了。

  • 您的感覺是正確的,您的同事是錯誤的(盡管這是一個合理的錯誤!)。 量角器自動等待一個WebDriver命令解析,然后再運行第二個。 因此,在你的第二個代碼塊, 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()內沒有什么不同
  • 我認為沒有最佳做法指南。 黃瓜不是量角器團隊的核心重點,它的支持大部分由GitHub上的社區提供。 如果您或您認識的某個人想編寫最佳實踐指南,我們(量角器團隊)將歡迎您參加PR!

對我有用的是-如果存在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM