Created
April 5, 2019 19:35
-
-
Save mike-zarandona/4c34b119efe2919eef20f096e26cc228 to your computer and use it in GitHub Desktop.
Cypress custom command to check if an element is in the viewport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cypress.Commands.add('shouldBeInViewport', { prevSubject: true }, subject => { | |
cy.document().then(doc => { | |
let clientHeight = doc.documentElement.clientHeight | |
let viewportTop = doc.documentElement.scrollTop | |
let viewportBottom = viewportTop + clientHeight | |
let subjectTop = subject[0].offsetTop | |
cy.expect(subjectTop).to.be.at.least(viewportTop) | |
cy.expect(subjectTop).to.be.at.most(viewportBottom) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment