Cucumber-js和Chai如何期望DOM中是否存在具有给定选择器的元素 [英] cucumber-js and Chai how to expect if element with given selector exist in DOM

查看:92
本文介绍了Cucumber-js和Chai如何期望DOM中是否存在具有给定选择器的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Cucumberjs有问题。我无法找到一种方法来确保具有给定选择器的元素
出现在DOM中。我在和Chai一起使用
黄瓜。
https://github.com/cucumber/cucumber-js
isPresent返回对象-否元素是否存在无关紧要。
因此,问题是如何检查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。 isPresent()
返回一个承诺,该承诺将解决该元素是否在页面上。

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;   
});

或使用带许诺的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中的几乎所有函数都返回承诺,这些承诺将解析为您实际要测试的值。因此,如果您只是尝试执行以下操作,则它将始终失败,因为它在 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的插件可以处理这种情况。它提供了最终链,该链将为您解决承诺并声明所产生的价值。上面的示例如下所示:

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中是否存在具有给定选择器的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆