Georgi Naumov Georgi Naumov - 1 year ago 125
Javascript Question

cucumber-js and Chai how to expect if element with given selector exist in 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.
isPresent returns object - no matter if the element exists or not.
So the question is how to check if element is present in DOM.

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.

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('someId')).isPresent().then(function(isElementVisible) {

Or use chai with promises.


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.

Answer Source

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:


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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download