Cucumber-js and Chai, as expected if an element with this selector exists in the DOM

I have a problem with cucumbers. I cannot find a way to provide this element with the given selector presented in the DOM. I use cucumbers with tea. https://github.com/cucumber/cucumber-js isPresent returns an object - whether this element exists or not. So the question is how to check if an element is present in the DOM.

I will edit the question to share one lesson learned. I read the documentation and I want to thank Nathan Thompson. present () returns a promise that will decide if an element is present on the page.

http://angular.imtqy.com/protractor/#/api?view=Protractor.prototype.isElementPresent

The code examples are a bit misleading. Therefore, if you want to expect that an element with this selector exists in the DOM, you should use something like this:

element(by.id('someId')).isPresent().then(function(isElementVisible) { expect(isElementVisible).to.be.true; }); 

Or use chai with promises.

 expect(element.isPresent()).to.eventually.be.false 

However, the word โ€œin the endโ€ sounds unpleasant. We want to be sure that in the end we are not sure. :)

Here you can view an article about this issue in my personal blog.

+7
javascript protractor cucumberjs chai
source share
2 answers

Almost all functions in Protractor return promises that will be resolved to the values โ€‹โ€‹you want to test. Therefore, if you are just trying to do something like the following, it will always fail because it claims the promise object returned by isPresent :

 expect(element.isPresent()).to.be.false 

I would recommend using the chai-as-promised plugin for Chai to handle such situations. It provides a chain of eventually that will allow promises for you and approve the result. The above example would look like this:

 expect(element.isPresent()).to.eventually.be.false 
+10
source share

Why, while waiting for a promise to return an item, do we use "false"? Maybe you mean: expect(element.isPresent()).to.be.true ?

0
source share

All Articles