Nightwatch: assert.visible returns true for offscreen elements

Created on 7 Jan 2020  路  4Comments  路  Source: nightwatchjs/nightwatch

I have a long page than can be scrolled and if after scroll I assert that scrolled away element is visible nightwatch always returns true.

I tried the same with pure js and js solution works fine:

isInViewport = function (elem) {
    var bounding = elem.getBoundingClientRect();
    return (
        bounding.top >= 0 &&
        bounding.left >= 0 &&
        bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
};

tested in chrome

I'm not sure that this is a bug, but according to description of assert visible function it is.

stale

Most helpful comment

Yes, this is kind of the only way to do it at the moment. Webdriver doesn't provide a built-in mechanism to check for offscreen elements, but we will add something like this in Nightwatch v2 for the current isVisible command, and probably change the existing isVisible to being called isDisplayed

All 4 comments

I'm having the same issue.

module.exports = {
  'My Page': (browser) => {
    browser
      .url('https://example.com')
      .moveToElement('footer', 0, 0)
      .assert.not.visible('#navigation-bar')
      .end()
  }
}

In the test, I watch Nightwatch jump to the bottom of the page and the navbar is definitely outside of the viewport, but the test fails with:

Testing if element <#navigation-bar> is not visible in 5000ms - expected "is not visible" but got: "visible" (5455ms)

I'm using the latest version of Nightwatch (1.3.4) and the latest Chromedriver (80.0.1).

By way of a workround I am doing this:

module.exports = {
  'My Page': (browser) => {
    browser
      .url('https://example.com')
      .moveToElement('footer', 0, 0)
      .execute(
        function getNavbarPOsition() {
          return document.querySelector('#navigation-bar').getBoundingClientRect().top;
        },
        [],
        function isOutsideViewport(result) {
          browser.assert.ok(result.value < 0, 'Navbar is off screen');
        },
      )
      .end()
  }
}

I'm not sure if this is the recommended way of doing things, but it works as expected.

Yes, this is kind of the only way to do it at the moment. Webdriver doesn't provide a built-in mechanism to check for offscreen elements, but we will add something like this in Nightwatch v2 for the current isVisible command, and probably change the existing isVisible to being called isDisplayed

i've also the need, just to demonstrate you're not alone :-)

This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chaseconey picture chaseconey  路  4Comments

Zechtitus picture Zechtitus  路  4Comments

betweenbrain picture betweenbrain  路  4Comments

davecranwell picture davecranwell  路  3Comments

manjero picture manjero  路  4Comments