Bird
Raised Fist0
Cypresstesting~10 mins

cy.scrollTo() in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to scroll the page to the bottom using Cypress.

Cypress
cy.scrollTo([1])
Drag options to blanks, or click blank then click option'
A"bottom"
B"top"
C"right"
D"left"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "top" scrolls to the top, not bottom.
Using directions like "left" or "right" scroll horizontally.
2fill in blank
medium

Complete the code to scroll the element with id 'content' to the top.

Cypress
cy.get('#content').scrollTo([1])
Drag options to blanks, or click blank then click option'
A"bottom"
B"right"
C"center"
D"top"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "bottom" scrolls to the bottom instead.
Using "center" scrolls to the middle, not top.
3fill in blank
hard

Fix the error in the code to scroll to the right side of the page.

Cypress
cy.scrollTo([1])
Drag options to blanks, or click blank then click option'
A"bottom"
B"right"
C"left"
D"top"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "left" scrolls to the left side.
Using "bottom" scrolls vertically down.
4fill in blank
hard

Fill both blanks to scroll the element with class 'box' to the center horizontally and vertically.

Cypress
cy.get('.box').scrollTo([1], [2])
Drag options to blanks, or click blank then click option'
A"center"
B"top"
C"left"
D"bottom"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing "top" or "bottom" with horizontal scroll.
Using different values for horizontal and vertical when centering.
5fill in blank
hard

Fill all three blanks to scroll the element with id 'panel' to the bottom right corner smoothly.

Cypress
cy.get('#panel').scrollTo([1], [2], { easing: [3] })
Drag options to blanks, or click blank then click option'
A"right"
B"bottom"
C"linear"
D"top"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping vertical and horizontal directions.
Using "top" instead of "bottom" for vertical scroll.
Using invalid easing values.

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