8

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.

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

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

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.

Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
  • 2
    Where is the `isPresent` method defined? CucumberJS or Chai? – Greg Burghardt Jun 10 '15 at 14:03
  • isPresent is method from protractor API. – Georgi Naumov Jun 11 '15 at 06:26
  • According to the [isPresent](http://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isPresent) documentation, it only tests whether or not the element exists in the document tree. Are you trying to detect whether the element is visible instead? An element can be invisible, but still "present" in the document tree. – Greg Burghardt Jun 11 '15 at 12:17

2 Answers2

10

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

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
Nathan Thompson
  • 2,354
  • 1
  • 23
  • 28
-1

Why when expecting the promise to return element we use “false”? Maybe you mean: expect(element.isPresent()).to.be.true?

msanford
  • 11,803
  • 11
  • 66
  • 93
Syslog
  • 1
  • 2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. If you [earn](//meta.stackoverflow.com/q/146472/169503) sufficient [reputation](//stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](//stackoverflow.com/help/privileges/comment). – Baum mit Augen Jul 12 '17 at 13:17