問題描述
我對 cucumberjs 有疑問.我找不到方法來確保具有給定選擇器的元素被呈現到 DOM 中.我正在使用cucumberjs 與柴.https://github.com/cucumber/cucumber-jsisPresent 返回對象 - 無論元素是否存在.所以問題是如何檢查元素是否存在于 DOM 中.
I have a problem with cucumberjs. I cannot find a way to ensure that element with given selector is presented into DOM. I'm using cucumberjs with Chai. https://github.com/cucumber/cucumber-js isPresent returns object - no matter if the element exists or not. So the question is how to check if element is present in DOM.
我將編輯問題以分享一個經驗教訓.我閱讀了文檔還要感謝 Nathan Thompson.存在()返回一個承諾,該承諾將解決該元素是否存在于頁面上.
I will edit the question to share one learned lesson. I read the documentation also want to thanks to Nathan Thompson. isPresent() returns a promise that will resolve to whether the element is present on the page.
http://angular.github.io/protractor/#/api?view=Protractor.prototype.isElementPresent
代碼示例有點誤導.因此,如果您想知道 DOM 中是否存在具有給定選擇器的元素,您必須使用如下內容:
The code examples is a little misleading. So if you want to expect if element with a given selector exist in DOM you must use something like this:
element(by.id('someId')).isPresent().then(function(isElementVisible) {
expect(isElementVisible).to.be.true;
});
或者使用帶有 promise 的 chai.
Or use chai with promises.
expect(element.isPresent()).to.eventually.be.false
但是,最終"這個詞聽起來令人不快.我們希望確定最終不確定.:)
However, the word "eventually" sounds unpleasant. We want to be sure not eventually sure. :)
這里可以在我的個人博客中查看了有關此問題的文章.
Here can be viewed article about this question into my personal blog.
推薦答案
Protractor 中幾乎所有的函數都返回 promise,這些 promise 將解析為您實際想要測試的值.因此,如果您只是嘗試執行以下操作,它總是會失敗,因為它是在 isPresent
返回的 Promise 對象上聲明:
Almost all functions in Protractor return promises that will resolve into the values you actually want to test against. So if you're just trying to do something like the following, it will always fail because it's asserting on the promise object returned by isPresent
:
expect(element.isPresent()).to.be.false
我建議使用 chai-as-promised 插件來處理 Chai像這樣的情況.它提供了 eventually
鏈,該鏈將為您解析 Promise 并對結果值進行斷言.上面的例子看起來像這樣:
I would recommend using the chai-as-promised plugin for Chai to handle situations like this. It provides the eventually
chain that will resolve promises for you and assert on the resulting value. The above example would look like this:
expect(element.isPresent()).to.eventually.be.false
這篇關于cucumber-js 和 Chai 如何期待具有給定選擇器的元素是否存在于 DOM 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!