kitimenpolku
kitimenpolku

Reputation: 2604

Nightwatch - Meaning of WaitForElementVisible and WaitForElementPresent

Im using Nightwatch for e2e and Im finding these two command a little bit confusing given the name and explanation that comes with them.

.waitForElementVisible: Waits a given time in milliseconds for an element to be visible in the page before performing any other commands or assertions.

What is the meaning of visible?

.waitForElementPresent: Waits a given time in milliseconds for an element to be present in the page before performing any other commands or assertions.

What is the meaning of present?

Is there any relation/implication between these two command?

A lot of questions but maybe an explanation about how they work would solve all of these small questions...

Sometimes Im just getting errors and Im thinking that it might be my bad understanding of these two commands.

Upvotes: 5

Views: 10959

Answers (3)

bytrangle
bytrangle

Reputation: 1139

At the time of my writing, Nightwatch didn't explain the difference between "visible" and "present". After digging their source code, I've noticed:

Both waitForElementVisible and waitForElementPresent commands rely on selenium-webdriver under the hood to search for the first element that matches the given selector. See locator.

However, waitForElementVisible takes another action in the queue: call this.elementFound(), which leads to calling this.executeProtocolAction('isElementDisplayed')

This does a bunch of plumbing work under the hood, which eventually leads to calling Selenium's method isDisplayed(). The function you want to look for in the source code of Selenium is bot.dom.isShown. The comments are quite detailed regarding what Selenium considers as being "shown". Some common scenrios:

  • hidden input, noscript elements are not considered shown
  • option elements are shown if the parent select element is shown
  • elements with visibility set to collapse or hidden are not shown.

I think the main mistake regarding waitForElementVisible and waitForElementPresent is when you use the former because it is commonly used, but your element is in fact not considered visible/shown by Selenium, so the test fails unexpectedly. If you want to assert that the element is in the DOM and don't care whether it is "visible", use waitForElementPresent.

This is an important point, as sometimes your test may fail because the element is in the DOM, but not visible on the web page, and you incorrectly waitForElementVisible, while you should have gone for waitForElementPresnet.

Upvotes: 0

Asoc
Asoc

Reputation: 51

Basically an element might be present (as in loaded in DOM) but not visible (e.g. out of view so you might need to scroll to see it, or it might be hidden).

If you want to perform an action like a click on an element then you want to be using 'visible'.

Upvotes: 0

Bao Tran
Bao Tran

Reputation: 626

What is problem with definition ? You already answer your question.

An element position in the footer, you need to scroll to see it, is it considered visible?

  • No,you dont need to scroll to check it visible or not.May be you do need with some command but not with these visible/present commands.

Does it mean visible in the DOM even if it is display:hidden, position:relative; left:20000px;, ...? Not actually visible for a user but dom is existing basically.

  • Yes,it exists(means present) in the DOM but for some reason it is not visible yet (bad connection,attribute value,...).

If an element return truth for .waitForElementVisible does it imply that .waitForElementPresent will return true?

  • yes it will, if an element is visible = > it is present.

For usage , you can check out my an example answer here,it might help .

Login timeout

Upvotes: 1

Related Questions