Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to element using Nightwatch?

I am using nightwatch for e2etesting my app. One of the tests fails because it cannot scroll to the element that it is testing I suspect. Question do I need to scroll or is there another way to do it? This is the element I am testing:

 return this.waitForElementVisible('#myElement', 4000) //wait for it to be visible
       .assert.visible('#myElement')
       .click('#myElement')

The element is at the top of the page but the testrunner has scrolled a bit down the page and it is not visible in the screenshot. How can I if need be scroll to this element? or : How can I test this element?

like image 906
bier hier Avatar asked Mar 28 '17 01:03

bier hier


People also ask

How do you scroll on Nightwatch?

url("http://example.com") . waitForElementPresent('body', 2000, "Be sure that the page is loaded") . execute('scrollTo(x, y)') .

How do I scroll until the element is visible?

Example. Code Implementation with Javascript Executor. While working with Actions class to scroll to view, we have to use the moveToElement() method. This method shall perform mouse movement till the middle of the element.

What is the command to run the script Nightwatch?

You can add Nightwatch to your project simply by running npm install nightwatch --save-dev . This places the Nightwatch executable in your ./node_modules/.

Is Nightwatch Selenium based?

Nightwatch. js framework is a Selenium-based test automation framework, written in Node. js and uses the W3C WebDriver API (formerly Selenium WebDriver).


1 Answers

There is a native method in nightwatch to get elements into view. (In general elements should always be scrolled into view from nightwatch/selenium. But if you want to do that by hand you can use getLocationInView():

return this.getLocationInView('#myElement')
   .assert.visible('#myElement')
   .click('#myElement')

Nightwatch also supports doing this directly via the Webdriver Protocol using moveTo() without any abstraction. In that case it would look more like:

const self = this;
return this.api.element('#myElement', (res) => {
  self.api.moveTo(res.value.ELEMENT, 0, 0, () => {
    self.assert.visible('#myElement');
    self.click('#myElement');
  })
});

(this was just written from top of my head, hope I didn't make a mistake)

But what could help in your case is changing seleniums element scroll behaviour in the config like:

firefox: {
  desiredCapabilities: {
    browserName: 'firefox',
    javascriptEnabled: true,
    acceptSslCerts: true,
    elementScrollBehavior: 1
  }
}

Default is 0 -> Elements are scrolled to top of page

elementScrollBavior 1 -> Elements are scrolled to bottom of page

like image 169
TheBay0r Avatar answered Sep 21 '22 10:09

TheBay0r