Skip to content

A small example showing how to use recursion to avoid JavaScript loops in Cypress

Notifications You must be signed in to change notification settings

bahmutov/avoid-while-loops-in-cypress

Repository files navigation

avoid-while-loops-in-cypress

A small example showing how to use recursion to avoid JavaScript loops in Cypress

In the plugins file we have a task that returns a random digit after 1 second delay. We want to call cy.task until it returns 7. See several solutions in the cypress/integration/spec.js file.

Repeating cy.task command until we get number 7

cypress-recurse

I have made this code into a reusable function, available on NPM under cypress-recurse. See the repository bahmutov/cypress-recurse.

Videos

I have explained the solution to this problem and refactored it several times to add more features, see the following videos

  1. Call cy task until it returns an expected value
  2. Reusable recursive function
  3. Reusable function with attempts limit
  4. Recursion function with time limit
  5. Convert recurse to use options object
  6. Add JSDoc types to the options parameter

See also

  • The recipe "Page reloads" reloads the page until it shows the number 7

About

A small example showing how to use recursion to avoid JavaScript loops in Cypress

Topics

Resources

Stars

Watchers

Forks