nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.75k stars 1.3k forks source link

isPresent command misleading, works same as waitUntilElementPresent #4185

Closed reallymello closed 2 months ago

reallymello commented 2 months ago

Description of the bug/issue

When I use const elementIsHere = browser.element('#myElement').isPresent() I expect a boolean of true or false to be returned to the elementIsHere constant variable, but if the element is not present in the DOM within the timeout the test returns an element not found exception. Suppress not found errors does not help in this instance. It's implied through the name it should return true if present and false if not.

There are cases in our testing where we sometimes need to branch logic when there are A/B versions of the UI served so the test can handle it. So, for example, if "something" is present then do this / else do something else.

.isPresent in its current form works the same as .waitUntilElementPresent() which I'd argue serve or should serve different purposes--the former should handle cases where the element may never be present where the latter expects that it should eventually appear within the timeout.

As a workaround I end up using findElements with a compound selector of something guaranteed to be in the DOM and the selector I'm waiting for then I see if the length of the elements found is greater than 1 which works but not as direct.

Steps to reproduce

In a test use

const elementIsHere = browser.element('#this-doesnt-exist').isPresent()

The test will throw a not found exception instead of setting elementIsHere to false.

Sample test

No response

Command to run

No response

Verbose Output

No response

Nightwatch Configuration

No response

Nightwatch.js Version

3.5

Node Version

20

Browser

Chrome 123

Operating System

Windows 11

Additional Information

No response

AutomatedTester commented 2 months ago

Looking at this, since you're using element(...) it's not really getting to the point where it does isPresent is called. element(...) will do auto waiting for you which leads to it acting like waitUntilElementPresent. This is

closing this as by design.

garg3133 commented 2 months ago

We also have https://github.com/nightwatchjs/nightwatch/issues/4055 that is going to serve exactly that purpose, it would only return true or false results instead of throwing an error when the element is not found.

There is already an active PR on the issue which we hope to merge and release soon.