Bird
Raised Fist0
Cypresstesting~15 mins

cy.scrollTo() in Cypress - Build an Automation Script

Choose your learning style10 modes available
Verify scrolling to the bottom of a long page
Preconditions (1)
Step 1: Open the web page URL 'https://example.com/long-page'
Step 2: Scroll to the bottom of the page using the scroll bar or mouse wheel
Step 3: Observe the page content at the bottom
✅ Expected Result: The page scrolls smoothly to the bottom, and the footer element with id 'footer' is visible
Automation Requirements - Cypress
Assertions Needed:
Verify the page scrolls to the bottom
Verify the footer element with id 'footer' is visible after scrolling
Best Practices:
Use cy.scrollTo() with 'bottom' argument to scroll
Use cy.get() with proper selector for footer
Use .should('be.visible') assertion to confirm visibility
Avoid hardcoded waits; rely on Cypress automatic waits
Automated Solution
Cypress
describe('Scroll to bottom test', () => {
  it('should scroll to the bottom and verify footer visibility', () => {
    cy.visit('https://example.com/long-page');
    cy.scrollTo('bottom');
    cy.get('#footer').should('be.visible');
  });
});

The test opens the long page URL using cy.visit(). Then it uses cy.scrollTo('bottom') to scroll the page to the bottom smoothly. After scrolling, it locates the footer element by its id #footer using cy.get(). Finally, it asserts that the footer is visible on the screen with .should('be.visible'). This confirms the scroll action worked as expected.

Common Mistakes - 3 Pitfalls
Using hardcoded waits like cy.wait(5000) before checking footer visibility
{'mistake': "Using incorrect selector like cy.get('.footer') when footer has id 'footer'", 'why_bad': 'Wrong selectors cause tests to fail because elements are not found', 'correct_approach': "Use accurate selectors matching the actual element attributes, e.g., cy.get('#footer')"}
Not using cy.scrollTo() and trying to scroll by triggering keyboard events
Bonus Challenge

Now add data-driven testing to scroll to different positions: 'top', 'center', and 'bottom' and verify footer visibility only at 'bottom'

Show Hint

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