You can use recursion in Cypress to continuously scroll until a specific element is found on the page. Here is an example:
index.tsx628 chars21 lines
Here, elementSelector
is the CSS selector for the element you are looking for. maxScrolls
is the maximum number of times to scroll before giving up (default is 10), and currentScroll
is the current number of times the function has scrolled (default is 0).
First, we try to get the element. If it exists, we return it. If it doesn't exist, we check if we have scrolled the maximum number of times. If we have, we throw an error. If we haven't, we scroll to the bottom of the page, wait for 500ms (adjustable), and then call the function recursively with an incremented currentScroll
. This will keep scrolling until the element is found or the maximum number of scrolls is reached.
gistlibby LogSnag