Bird
Raised Fist0
Cypresstesting~5 mins

cy.scrollTo() in Cypress

Choose your learning style10 modes available
Introduction

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.
Cypress
cy.scrollTo('center', { duration: 1000, easing: 'linear' })
Sample Program

This test opens a page, scrolls to the bottom, and checks if the footer is visible.

Cypress
describe('Scroll test example', () => {
  it('scrolls to bottom and checks footer visibility', () => {
    cy.visit('https://example.cypress.io')
    cy.scrollTo('bottom')
    cy.get('footer').should('be.visible')
  })
})
OutputSuccess
Important Notes

Use cy.scrollTo() to simulate real user scrolling behavior.

Scrolling can be animated or instant; animation helps test smooth scrolling effects.

Always check if the element you want to interact with is visible after scrolling.

Summary

cy.scrollTo() moves the page view to a specific position.

It helps test elements that appear only after scrolling.

You can scroll by position name or exact coordinates, with optional animation.

Practice

(1/5)
1. What is the main purpose of cy.scrollTo() in Cypress testing?
easy
A. To move the page view to a specific position for testing elements
B. To click on a button automatically
C. To wait for an element to appear
D. To type text into an input field

Solution

  1. Step 1: Understand the function's role

    cy.scrollTo() is used to scroll the page to a certain position.
  2. Step 2: Identify its testing use

    This helps test elements that only appear after scrolling.
  3. Final Answer:

    To move the page view to a specific position for testing elements -> Option A
  4. Quick Check:

    Scroll page = Move view [OK]
Hint: Think: scrolling moves the page view to test hidden elements [OK]
Common Mistakes:
  • Confusing scrollTo with click or type commands
  • Thinking scrollTo waits for elements
  • Assuming scrollTo changes element state
2. Which of the following is the correct syntax to scroll to the bottom of the page using cy.scrollTo()?
easy
A. cy.scrollTo('down')
B. cy.scrollTo('bottom')
C. cy.scrollTo('end')
D. cy.scrollTo('bottomRight')

Solution

  1. Step 1: Recall valid position names

    Valid positions include 'top', 'bottom', 'left', 'right', etc. 'bottom' scrolls to the page bottom.
  2. Step 2: Check each option

    'down' and 'end' are invalid; 'bottomRight' is a valid position in Cypress.
  3. Final Answer:

    cy.scrollTo('bottomRight') -> Option D
  4. Quick Check:

    Scroll bottomRight = 'bottomRight' [OK]
Hint: Use standard position names like 'top', 'bottom', or 'bottomRight' in quotes [OK]
Common Mistakes:
  • Using invalid position names like 'down' or 'end'
  • Forgetting quotes around position strings
  • Trying to use combined positions incorrectly
3. Given the code snippet:
cy.scrollTo(0, 500);
cy.get('#footer').should('be.visible');

What does cy.scrollTo(0, 500) do before checking the footer visibility?
medium
A. Scrolls to the top-left corner
B. Scrolls horizontally 500px and vertically 0px down
C. Scrolls horizontally 0px and vertically 500px down
D. Scrolls to the bottom of the page

Solution

  1. Step 1: Understand scrollTo(x, y) parameters

    The first number is horizontal (x), second is vertical (y) scroll in pixels.
  2. Step 2: Apply values from code

    Here, x=0 means no horizontal scroll, y=500 means scroll down 500 pixels vertically.
  3. Final Answer:

    Scrolls horizontally 0px and vertically 500px down -> Option C
  4. Quick Check:

    scrollTo(x=0, y=500) = vertical 500px down [OK]
Hint: First number is horizontal, second is vertical pixels [OK]
Common Mistakes:
  • Mixing up x and y values
  • Assuming scrollTo scrolls to bottom automatically
  • Thinking scrollTo(0, 500) scrolls horizontally
4. Identify the error in this Cypress code:
cy.scrollTo(bottom);
cy.get('.menu').should('be.visible');
medium
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

  1. Step 1: Check scrollTo argument syntax

    Position names like 'bottom' must be strings in quotes.
  2. Step 2: Verify other code parts

    Selector '.menu' is valid, semicolons are optional in JS, and scrollTo can be used before get().
  3. Final Answer:

    Missing quotes around 'bottom' in scrollTo argument -> Option A
  4. 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');
C. cy.scrollTo({ x: 'top', y: 'right' }, { smooth: true }); cy.get('#submit').should('be.visible');
D. cy.scrollTo('top', 'right', { smooth: true }); cy.get('#submit').should('be.visible');

Solution

  1. Step 1: Understand correct scrollTo syntax for smooth scroll

    Use position string like 'topRight' and options object with duration for animation.
  2. 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.
  3. Final Answer:

    cy.scrollTo('topRight', { duration: 1000 }); cy.get('#submit').should('be.visible'); -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard