Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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