Cypress
Course Index
Index

Cypress Advanced

1. How would you implement parallel test execution in Cypress to reduce test runtime?

Cypress supports parallel test execution when integrated with the Cypress Dashboard. To enable parallel execution:

  • Use the --parallel flag when running tests with the Cypress Dashboard:

npx cypress run --record --parallel --key <CYPRESS_DASHBOARD_KEY>
  • Use CI tools like GitHub Actions, Jenkins, CircleCI, or GitLab CI/CD to distribute test execution across multiple machines.
  • Shard test execution manually by splitting tests across multiple jobs in CI/CD.
  • Run independent tests in parallel using multiple terminal instances locally.
Limitation: Parallelization requires the Cypress Dashboard unless manually handled in CI/CD.

2. What is the Cypress Dashboard, and how can it help in managing and analyzing test runs?

The Cypress Dashboard is a cloud-based service that provides:

  • Test history and analytics: Track past test runs, failures, and trends.
  • Parallel test execution: Helps run tests faster across multiple machines.
  • Screenshots and video recordings: Helps debug failed tests.
  • Flaky test detection: Identifies unstable tests by analyzing reruns.
To integrate the Cypress Dashboard, register the project and use the --record flag when running tests.

3. How do you deal with flaky tests in Cypress, and what strategies can you use to debug them?

Flaky tests are tests that fail inconsistently. Strategies to handle flaky tests in Cypress:

  • Use Cypress's built-in retries in cypress.config.js:

Javascript
Copy
module.exports = {
  retries: {
    runMode: 2, // Retries in CI
    openMode: 0  // No retries when running locally
  }
};

  • Avoid hard-coded waits (cy.wait(5000)) and use Cypress’s automatic waiting.
  • Use proper assertions (should() instead of then()) for auto-retries.
  • Ensure stable test data by stubbing API responses (cy.intercept()).
  • Debug using Cypress time-travel feature, console logs, and screenshots.

4. Explain how to run Cypress tests in a Continuous Integration (CI) pipeline.

To run Cypress tests in a CI/CD pipeline:

  • Install Cypress in the CI/CD environment:

npm install cypress --save-dev
  • Run Cypress tests in headless mode:

npx cypress run --headless
  • Store test results in artifacts (JUnit, JSON, HTML reports).
  • Parallel execution:
npx cypress run --record --parallel --key <CYPRESS_DASHBOARD_KEY>
  • Example GitHub Actions workflow (.github/workflows/cypress.yml):

YAML
Copy
name: Cypress Tests
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: npm install
      - name: Run Cypress tests
        run: npx cypress run

5. How does Cypress handle cross-browser testing, and what are its current limitations?

Cypress supports Chrome, Edge, Firefox, and Electron for cross-browser testing.
To run tests in a specific browser:
npx cypress run --browser chrome
npx cypress run --browser firefox

Limitations:

  • No support for Safari or Internet Explorer.
  • Limited cross-browser compatibility features compared to Selenium.

6. How do you debug failing Cypress tests? Mention tools or techniques you would use.

Cypress provides multiple debugging techniques:

  • Time-travel debugging: Hover over commands in the Test Runner to see the previous state of elements.
  • Console logs: Use cy.log() and console.log() for debugging.
  • Pause execution:

Javascript
Copy
cy.get('.element').debug();  // Leaves the element in the console for manual inspection
cy.pause(); // Pauses execution in Cypress Test Runner

  • Screenshots and videos: Cypress captures screenshots on failures (cypress/screenshots/).
  • Use cy.intercept() to debug API calls and check request/response logs.

7. What is the purpose of Cypress's time-travel feature, and how can it help in debugging?

The time-travel debugging feature in Cypress allows viewing snapshots of test steps.

  • Each Cypress command appears in the Test Runner UI.
  • Hovering over a command shows the app’s state at that moment.
  • Great for debugging element interactions without rerunning the test.

Example:
Javascript
Copy
cy.get('.button').click().debug();

Here, debug() pauses execution, and Cypress lets users inspect elements before proceeding.

8. Explain how screenshots and videos can be used for debugging tests in Cypress.

Cypress captures:

  • Screenshots automatically on failures (cypress/screenshots/).
  • Videos of test execution (cypress/videos/).

To enable screenshots and videos, update cypress.config.js:
Javascript
Copy
module.exports = {
  video: true,
  screenshotOnRunFailure: true
};

Or manually take a screenshot: cy.screenshot('custom_name');
These artifacts help identify why tests failed, especially in CI/CD.

Unknown
Copy

9. How would you organize and structure large test suites in a Cypress project?

For large Cypress test suites:

  • Follow a modular folder structure:
cypress/ ├── integration/ │ ├── login-tests/ │ ├── dashboard-tests/ ├── fixtures/ ├── support/ │ ├── commands.js # Custom commands │ ├── index.js # Global configuration

  • Use Page Object Model (POM) for maintainability

Javascript
Copy
class LoginPage {
  visit() { cy.visit('/login'); }
  enterUsername(username) { cy.get('#username').type(username); }
  enterPassword(password) { cy.get('#password').type(password); }
  submit() { cy.get('button').click(); }
}
export default new LoginPage();

  • Use tags to categorize and group tests (e.g., Smoke, Regression, API).
  • Avoid unnecessary duplication with Cypress custom commands.

10. What are best practices for using tags to group or run specific tests in Cypress?

Cypress doesn't support tags natively, but plugins like cypress-grep help.
Install:
npm install --save-dev cypress-grep
Use tags in tests:

Javascript
Copy
describe('Login Tests', { tags: '@smoke' }, () => {
  it('Valid login', () => {
    cy.visit('/login');
    cy.get('#username').type('admin');
    cy.get('#password').type('password');
    cy.get('button').click();
  });
});

Run tests by tag:
npx cypress run --env grepTags=@smoke

11. How do you handle sensitive information like API keys or passwords in Cypress tests?

  • Use environment variables (cypress.env.json)

Json
Copy
{
  "apiKey": "your-secret-api-key"
}

Access it in tests:
Javascript
Copy
cy.request({
  url: '/api/data',
  headers: { Authorization: `Bearer ${Cypress.env('apiKey')}` }
});

  • Use CYPRESS_ENV in CI/CD pipelines instead of hardcoding credentials.
  • Never store secrets in test files!