Cypress: Standard scrollTo usage often results in errors

Created on 17 Oct 2017  路  12Comments  路  Source: cypress-io/cypress

Current behavior:

Running the example code, which performs a simple scrollTo often results in an error stating that multiple elements cannot be scrolled.

Desired behavior:

Don't error, and scroll the page. 馃ぁ

How to reproduce:

Run the example code.

Test code:

  context('Page Scroll', () => {
    it('should work', () => {
      cy.visit('https://www.theguardian.com/uk')
      cy.scrollTo('0%', '35%')
    })
  })

Additional Info (images, stack traces, etc)

screenshot 2017-10-17 16 03 22

  • Operating System: Mac OS 10.12.6
  • Cypress Version: 1.0.2
  • Browser Version: Chrome 61
1锔忊儯 first-timers-only pkdriver scrolling 鈫曪笍 bug

Most helpful comment

@ctrlplusb You could temporarily use vanilla js before having a real fix on scrollTo

cy.window().then(win => {
  win.scrollTo('0%', '35%');
})

All 12 comments

Verified this is bug.

Code for this command can be found here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/actions/scroll.coffee

My first impression is that we are either:

  1. Not testing this command on a website with multiple windows (iframes)
  2. Not passing the window subject correctly to the scrollTo command.

Any updates to this?

Also window.scrollTo isnt cross browser supported

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo

The implementation actually uses jQuery scrollTo, which I'd have to look back into to remember their implementation of.

@ctrlplusb You could temporarily use vanilla js before having a real fix on scrollTo

cy.window().then(win => {
  win.scrollTo('0%', '35%');
})

I'm no longer able to get this code example working. Can anyone provide a basic example of this scroll bug?

This is because window.length is equal to the number of iframes on the page, causing above assertion error if the page has iframe.

I reproduced it easily in my project by just having a google map on the page: it will create an iframe and the previously existing cy.scrollTo(0, 1); suddenly breaks.
We worked around it by not displaying the map on the e2e tests, not ideal, but does the job...

Here is a simple reproduction script:

describe('ScrollTo with iframe', () => {
  it('does not scroll', () => {
    cy.visit('https://www.nytimes.com');
    cy.scrollTo('bottom');
  });
});

Thanks @Kjir, I can recreate this again.

Screen Shot 2019-03-14 at 4 12 54 PM
It may be trying to scroll all windows within the page, so errors when iframes. This is a complete guess though.

Are there any other ways to scroll the page or please atleast fix this issue

The key to this issue was here: https://github.com/cypress-io/cypress/issues/751#issuecomment-465876618

This is because window.length is equal to the number of iframes on the page, causing above assertion error if the page has iframe.

This was fixed in https://github.com/cypress-io/cypress/pull/4397 and released in 3.3.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zbigniewkalinowski picture zbigniewkalinowski  路  3Comments

weskor picture weskor  路  3Comments

szabyg picture szabyg  路  3Comments

brian-mann picture brian-mann  路  3Comments

brian-mann picture brian-mann  路  3Comments