Bird
Raised Fist0
Cypresstesting~8 mins

Retry-ability of commands in Cypress - Framework Patterns

Choose your learning style10 modes available
Framework Mode - Retry-ability of commands
Folder Structure
  cypress/
  ├── e2e/                  # Test specs
  │   ├── login.cy.js       # Example test file
  │   └── dashboard.cy.js
  ├── fixtures/             # Test data files (JSON)
  │   └── users.json
  ├── support/              # Custom commands and utilities
  │   ├── commands.js       # Custom Cypress commands
  │   └── e2e.js            # Support file loaded before tests
  └── cypress.config.js     # Cypress configuration file
  
Test Framework Layers
  • Test Specs (e2e/): Contains test files that use Cypress commands. These commands automatically retry until they pass or timeout.
  • Support Layer (support/): Defines custom commands and utilities. Custom commands inherit Cypress's retry-ability when built properly.
  • Fixtures (fixtures/): Holds test data used by tests to keep tests clean and data-driven.
  • Configuration (cypress.config.js): Sets global timeouts and retry options for commands and tests.
Configuration Patterns

Retry-ability in Cypress is controlled via configuration and command design:

  • Global Retries: Set retries for tests and hooks in cypress.config.js using retries option.
  • Command Timeout: Adjust defaultCommandTimeout to control how long Cypress retries commands before failing.
  • Custom Command Design: Write custom commands using Cypress commands inside so retry-ability is preserved.
  • Environment Variables: Use env in config to toggle retries or timeouts per environment (e.g., CI vs local).
  // cypress.config.js example
  import { defineConfig } from 'cypress';

  export default defineConfig({
    e2e: {
      retries: 2, // retry failed tests twice
      defaultCommandTimeout: 8000, // 8 seconds retry per command
      env: {
        baseUrl: 'https://example.com'
      }
    }
  })
  
Test Reporting and CI/CD Integration
  • Retry Reporting: Cypress reports retries in the test runner UI and in CI logs, showing which attempts passed or failed.
  • CI/CD Integration: Configure CI pipelines (GitHub Actions, Jenkins, etc.) to run Cypress tests with retries enabled to reduce flaky failures.
  • Test Reports: Use reporters like Mochawesome to generate detailed HTML reports showing retries and test outcomes.
  • Flake Reduction: Retries help reduce false failures caused by temporary issues like network delays or slow UI rendering.
Best Practices for Retry-ability in Cypress
  1. Use Cypress Built-in Retries: Enable retries in cypress.config.js instead of custom retry logic.
  2. Write Idempotent Tests: Ensure tests can safely run multiple times without side effects to benefit from retries.
  3. Leverage Cypress Command Retry: Use Cypress commands for actions and assertions to get automatic retry support.
  4. Keep Custom Commands Simple: Build custom commands from Cypress commands to inherit retry-ability.
  5. Adjust Timeouts Wisely: Set command timeouts to balance between waiting for UI and test speed.
Self Check

Where in this folder structure would you add a new custom command that retries clicking a button until it becomes enabled?

Key Result
Cypress commands automatically retry until success or timeout, enabling stable and reliable test execution.

Practice

(1/5)
1. What does retry-ability in Cypress help with during test execution?
easy
A. Automatically waiting for elements or conditions before proceeding
B. Skipping tests that take too long to run
C. Running tests in parallel on multiple browsers
D. Generating test data automatically

Solution

  1. Step 1: Understand retry-ability concept

    Retry-ability means Cypress waits and retries commands until the element or condition is met or timeout occurs.
  2. Step 2: Identify what retry-ability helps with

    This automatic waiting avoids flaky tests caused by timing issues, ensuring tests proceed only when ready.
  3. Final Answer:

    Automatically waiting for elements or conditions before proceeding -> Option A
  4. Quick Check:

    Retry-ability = Automatic wait [OK]
Hint: Retry-ability means Cypress waits automatically for conditions [OK]
Common Mistakes:
  • Confusing retry-ability with parallel test execution
  • Thinking retry-ability skips tests
  • Assuming retry-ability generates test data
2. Which of the following Cypress command patterns correctly uses retry-ability?
easy
A. cy.wait(5000).get('.btn').click()
B. cy.get('.btn').click()
C. cy.click('.btn')
D. cy.get('.btn').should('be.visible').click()

Solution

  1. Step 1: Identify retry-ability usage

    Retry-ability works with commands like get combined with should assertions that retry until condition is true.
  2. Step 2: Check each option

    cy.get('.btn').should('be.visible').click() uses get with should('be.visible') which retries until the button is visible before clicking.
  3. Final Answer:

    cy.get('.btn').should('be.visible').click() -> Option D
  4. Quick Check:

    Use get + should for retry-ability [OK]
Hint: Use get() with should() for retry-ability in Cypress [OK]
Common Mistakes:
  • Using cy.click() directly without get()
  • Relying on fixed wait times instead of should()
  • Missing should() causes no retry
3. Consider this Cypress code snippet:
cy.get('#status').should('contain', 'Success')
cy.get('#submit').click()

What happens if the element with id status initially does not contain 'Success' but updates after 2 seconds?
medium
A. The test fails immediately because 'Success' is not found
B. The test skips the assertion and clicks submit immediately
C. Cypress retries the assertion until 'Success' appears or timeout
D. The test waits 2 seconds automatically before clicking submit

Solution

  1. Step 1: Understand retry-ability with should()

    The should('contain', 'Success') assertion retries until the text appears or timeout.
  2. Step 2: Analyze behavior when text updates after 2 seconds

    Cypress waits and retries the assertion, so it passes once 'Success' appears, then proceeds to click.
  3. Final Answer:

    Cypress retries the assertion until 'Success' appears or timeout -> Option C
  4. Quick Check:

    should() retries until condition met [OK]
Hint: should() retries assertions until success or timeout [OK]
Common Mistakes:
  • Assuming test fails immediately without retry
  • Thinking Cypress waits fixed time automatically
  • Believing test skips assertion
4. You wrote this Cypress test:
cy.get('.loading').should('not.exist')
cy.get('.content').click()

But the test fails because .content is not clickable yet. What is the likely cause?
medium
A. The get('.content') command does not wait for element to be clickable
B. The should('not.exist') does not retry properly for .loading
C. You need to add wait() before clicking .content
D. The selector .content is incorrect

Solution

  1. Step 1: Understand retry-ability of should('not.exist')

    The should('not.exist') retries until the loading element disappears, so this part works correctly.
  2. Step 2: Analyze get('.content').click() behavior

    The get command retries until the element exists but does not wait for it to be clickable or visible before clicking.
  3. Final Answer:

    The get('.content') command does not wait for element to be clickable -> Option A
  4. Quick Check:

    get() waits for existence, not clickability [OK]
Hint: get() waits for element existence, not clickability [OK]
Common Mistakes:
  • Assuming should('not.exist') fails to retry
  • Adding unnecessary fixed waits
  • Blaming wrong selector without checking
5. You want to test a button that appears only after a loading spinner disappears. Which Cypress command sequence best uses retry-ability to ensure the button is clicked only when ready?
hard
A. cy.get('.spinner').should('not.exist') cy.get('button#submit').click()
B. cy.get('.spinner').should('not.exist') cy.get('button#submit').should('be.visible').click()
C. cy.get('button#submit').should('be.visible').click()
D. cy.wait(3000) cy.get('button#submit').click()

Solution

  1. Step 1: Wait for spinner to disappear

    Using should('not.exist') on spinner retries until spinner is gone, ensuring loading finished.
  2. Step 2: Wait for button to be visible before clicking

    Using should('be.visible') retries until button is visible and ready for click, avoiding flaky clicks.
  3. Final Answer:

    cy.get('.spinner').should('not.exist') cy.get('button#submit').should('be.visible').click() -> Option B
  4. Quick Check:

    Chain should() assertions for reliable waits [OK]
Hint: Chain should('not.exist') and should('be.visible') for reliable clicks [OK]
Common Mistakes:
  • Clicking button immediately after spinner disappears without visibility check
  • Using fixed wait instead of retry-ability
  • Skipping spinner wait and causing flaky tests
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