We use cy.scrollTo() to move the page view to a specific spot. This helps us test parts of a page that are not visible without scrolling.
When you want to test a button or link that is below the visible area of the page.
When you need to check if a footer or header appears correctly after scrolling.
When testing lazy-loaded images or content that appear only after scrolling.
When verifying that scrolling triggers certain animations or events.
When automating user actions that require scrolling to a specific section.
Syntax
Cypress
cy.scrollTo(position, options)
// position can be a string like 'top', 'bottom', 'left', 'right', 'center'// or an object with x and y coordinates like { x: 0, y: 250 }// options is an optional object for animation and easing
The position argument tells Cypress where to scroll.
You can scroll instantly or with animation by setting options.
Examples
This scrolls the page all the way down to the bottom.
Cypress
cy.scrollTo('bottom')
This scrolls the page back up to the top.
Cypress
cy.scrollTo('top')
This scrolls vertically 500 pixels down from the top.
Cypress
cy.scrollTo({ x: 0, y: 500 })
This scrolls smoothly to the center of the page over 1 second.
A. Missing quotes around 'bottom' in scrollTo argument
B. Incorrect selector '.menu' used
C. Missing semicolon after scrollTo
D. scrollTo cannot be used before get()
Solution
Step 1: Check scrollTo argument syntax
Position names like 'bottom' must be strings in quotes.
Step 2: Verify other code parts
Selector '.menu' is valid, semicolons are optional in JS, and scrollTo can be used before get().
Final Answer:
Missing quotes around 'bottom' in scrollTo argument -> Option A
Quick Check:
Position names need quotes [OK]
Hint: Always put position names in quotes in scrollTo() [OK]
Common Mistakes:
Forgetting quotes around position strings
Thinking semicolons are mandatory
Assuming scrollTo must come after get()
5. You want to scroll smoothly to the top-right corner of a page and then check if a button with id #submit is visible. Which of the following code snippets correctly achieves this?
hard
A. cy.scrollTo('topRight', 1000); cy.get('#submit').should('be.visible');
B. cy.scrollTo('topRight', { duration: 1000 }); cy.get('#submit').should('be.visible');
D. cy.scrollTo('top', 'right', { smooth: true }); cy.get('#submit').should('be.visible');
Solution
Step 1: Understand correct scrollTo syntax for smooth scroll
Use position string like 'topRight' and options object with duration for animation.
Step 2: Evaluate each option
A uses correct syntax with position string and options object. B passes duration as second argument incorrectly. C uses invalid x/y strings and option name. D passes multiple strings incorrectly.
Final Answer:
cy.scrollTo('topRight', { duration: 1000 }); cy.get('#submit').should('be.visible'); -> Option B
Quick Check:
Position string + options object = correct [OK]
Hint: Use position string and options object for smooth scroll [OK]
Common Mistakes:
Passing duration as a separate argument instead of options object
Using multiple position strings instead of one combined string
Using invalid option names like 'smooth'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.