Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll element into view when using .should('be.visible') #2892

Closed
suchipi opened this issue Dec 4, 2018 · 4 comments
Closed

Scroll element into view when using .should('be.visible') #2892

suchipi opened this issue Dec 4, 2018 · 4 comments

Comments

@suchipi
Copy link

suchipi commented Dec 4, 2018

If you have a scrollable list of items, and want to verify that one of the items is in the list, the following isn't sufficient in all cases because it doesn't cause Cypress to scroll the element into view:

cy.contains('Hi').should('be.visible');

However, you can work around this by triggering an event on the element, which activates the scroll algorithm documented here:

cy.contains('Hi').trigger('dummy-event').should('be.visible');

Would the Cypress team be interested in a PR making a change such that this scroll algorithm is called when .should is called with 'be.visible'?

Current behavior:

Automatic scrolling does not occur when using .should('be.visible').

Desired behavior:

Automatic scrolling occurs when using .should('be.visible').

Versions

Cypress 3.1.1, macOS 10.13.6, Chrome 70

@andezzat
Copy link
Contributor

andezzat commented Dec 5, 2018

I've had this 'issue' when doing a set of asserts on multiple list/item els inside a parent container.
I had a workaround, which was to issue cy.scrollIntoView() every time...

Before interacting with an element, we will always scroll it into view (including any of its parent containers). Even if the element was visible without scrolling, we perform the scrolling algorithm in order to reproduce the same behavior every time the command is run.
Source: https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html#Scrolling

The question then becomes, does a should assert count as an 'interaction'. If so, then it SHOULD scroll into view before asserting; otherwise, it's working as expected...

@jennifer-shehane Can you advise please?

@suchipi
Copy link
Author

suchipi commented Dec 6, 2018

After thinking about this some more, I realized this could be undesirable in situations where you want to assert that the app itself scrolls the content into view.

I didn't know that there was a scrollIntoView command; with that in mind, I don't think we should change this.

@suchipi suchipi closed this as completed Dec 6, 2018
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Dec 6, 2018

@andezzat The list of commands here are what we consider an 'interaction'.

@suchipi Yes, we did refactor our visibility assertions over a year ago to only take into account the current viewport, so that scrolling to the element is first required before Cypress determining it as visible.

@croraf
Copy link

croraf commented Jul 10, 2020

I hope that a similar command can be introduced in Cypress api, like .should('be.scrollVisible') or .should('be.scrollable') or similar, which would assert that you can make the element visible after scrolling, and which would scroll to it if so. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants