Skip to content

Instantly share code, notes, and snippets.

@mike-zarandona
Created April 5, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-zarandona/4c34b119efe2919eef20f096e26cc228 to your computer and use it in GitHub Desktop.
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
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