April 4, 2025
|
10
mins

The Ultimate Cypress Testing Toolkit for Developers

Saravanakumar Umapathi

Introduction

Cypress is a modern end-to-end testing framework designed for web applications. While it is primarily used for UI testing, Cypress can also be leveraged for testing document file uploads and interactions.

Why Use Cypress for Document File Testing?

  • Easy setup and execution
  • Real-time execution with automatic reloading
  • Supports file uploads and downloads
  • Integration with CI/CD pipelines

Framework in Cypress

In Cypress, you can structure your test automation framework in different ways based on your project needs. The main types of frameworks used in Cypress include:

1. Behavior-Driven Development (BDD) Framework

  • Uses Cucumber with Cypress (cypress-cucumber-preprocessor)
  • Tests are written in Gherkin syntax (Given-When-Then)
  • Enhances collaboration between QA, Devs, and Business teams

Example:

Feature: Login Functionality 
               Scenario: Successful Login 
                  Given I visit the login page 
                  When I enter valid credentials 
                  Then I should be redirected to the dashboard 

2. Page Object Model (POM) Framework

  • Separates test logic from UI element locators
  • Improves test maintenance and reusability
  • Uses JavaScript/TypeScript classes to represent UI pages

Example:

class LoginPage { 
  visit() { cy.visit('/login'); } 
  enterUsername(username) { cy.get('#username').type(username); } 
  enterPassword(password) { cy.get('#password').type(password); } 
  clickLogin() { cy.get('#loginButton').click(); } 
} 
 

Usage in a test: 

const loginPage = new LoginPage(); 
loginPage.visit(); 
loginPage.enterUsername('user123'); 
loginPage.enterPassword('password'); 
loginPage.clickLogin(); 

3. Data-Driven Framework

  • Uses external data sources like JSON, CSV, or Excel
  • Useful for running the same test with multiple data sets

Example:

cy.fixture('users.json').then((users) => { 
  users.forEach((user) => { 
    cy.login(user.username, user.password); 
  }); 
}); 

4. Keyword-Driven Framework

  • Uses keywords to represent actions instead of writing full Cypress scripts
  • Keywords like login, addToCart, logout are predefined

Example:

const actions = { 
  login: () => cy.get('#loginButton').click(), 
  logout: () => cy.get('#logoutButton').click(), 
}; 
actions.login(); 

5. Hybrid Framework

  • Combines multiple frameworks (e.g., POM + Data-Driven + BDD
  • Offers flexibility for larger test suites

Each framework type can be used based on project requirements. Let me know if you need guidance on setting up any of these in Cypress!

Browsers Supported by Cypress

Cypress supports multiple browsers, allowing you to run tests in different environments.

  • Electron is the default browser used in Cypress if no browser is specified.
  • Firefox support requires Cypress v4.0+.
  • Edge Chromium support was added in v4.0+.

Browsers NOT Supported in Cypress

  • Internet Explorer (IE)
  • Safari (WebKit support is experimental)
  • Opera

Tools for Cypress

  1. Visual Studio Code (VS Code) – Used for writing Cypress test scripts.
  2. Node.js – Required for running Cypress as it is a JavaScript-based testing framework.
  3. Cypress Test Runner – The core Cypress framework to execute tests.
  4. Mocha – Test framework used by Cypress.
  5. Chai – Assertion library used for Cypress tests.
  6. Cypress Plugins – Additional functionalities like API testing, custom commands, etc.

Installing Cypress

To get started with Cypress, install it using npm:

npm install --save-dev cypress 

Run Cypress with:

npx cypress open

Testing File Uploads in Cypress

Cypress provides built-in support for file uploads. You can use the cy.get('input[type=file]') method and the cypress-file-upload plugin.

Installation

npm install --save-dev cypress-file-upload

Example Test:

import 'cypress-file-upload'; 
 
describe('Document Upload Test', () => { 
    it('should upload a document file', () => { 
        cy.visit('/upload-page'); 
        cy.get('input[type=file]').attachFile('sample.docx'); 
        cy.get('#upload-button').click(); 
        cy.contains('Upload Successful').should('be.visible'); 
    }); 
}); 

Testing File Downloads in Cypress

To verify a file download, you can use cy.readFile() to check if the file exists in the downloads folder.

Example Test:

describe('Document Download Test', () => { 
    it('should download a document file', () => { 
        cy.visit('/download-page'); 
        cy.get('#download-button').click(); 
        cy.readFile('cypress/downloads/sample.docx').should('exist'); 
    }); 
}); 
 

Running Tests in CI/CD

To integrate Cypress into your CI/CD pipeline, use the following command:

npx cypress run 

For GitHub Actions, add the following workflow:

name: Cypress Tests 
on: [push, pull_request] 
 
jobs: 
  cypress-run: 
    runs-on: ubuntu-latest 
    steps: 
      - uses: actions/checkout@v3 
      - name: Install Dependencies 
        run: npm install 
      - name: Run Cypress Tests 
        run: npx cypress run 

Cypress Reporting: Setup & Configuration for Automated Testing

Cypress is a powerful automation tool for web testing, but it lacks built-in reporting features. Generating detailed test reports is essential for tracking test results, debugging failures, and integrating with CI/C D pipelines. In this guide, we’ll set up Mochawesome and JUnit reporters to generate beautiful, interactive reports for Cypress tests.

1. Installing Required Packages

To enable reporting in Cypress, install the necessary dependencies using npm:

npm install --save-dev mocha mocha-junit-reporter mochawesome mochawesome-merge mochawesome-report-generator 

These packages allow Cypress to generate JSON and HTML reports.

2. Configuring Cypress to Use Mochawesome Reports

Modify the Cypress configuration file cypress.config.js (or cypress.config.ts for TypeScript users):

const { defineConfig } = require("cypress"); 
 
module.exports = defineConfig({ 
  reporter: "mochawesome", 
  reporterOptions: { 
    reportDir: "cypress/reports", 
    overwrite: false, 
    html: true, 
    json: true 
  } 
}); 
 

Why Mochawesome?

  • Generates detailed HTML reports
  • Provides screenshots and logs for failures
  • Supports merging multiple test reports

3. Generating Merged Reports

Since Cypress runs tests in parallel, multiple JSON reports are generated. We need to merge them into a single report.

Add Scripts in package.json

"scripts": { 
  "merge-reports": "mochawesome-merge cypress/reports/*.json > cypress/reports/mochawesome.json", 
  "generate-report": "marge cypress/reports/mochawesome.json -o cypress/reports" 
} 

Run Cypress Tests and Generate Reports

npx cypress run 
npm run merge-reports 
npm run generate-report 

This will create a beautiful HTML report inside the cypress/reports folder.

4. Generating JUnit Reports (for CI/CD Integration)

JUnit reports are useful for integrating Cypress test results into CI/CD tools like Jenkins, GitLab, or CircleCI.

Modify Cypress Configuration

Edit cypress.config.js to use the mocha-junit-reporter:

module.exports = defineConfig({ 
  reporter: "mocha-junit-reporter", 
  reporterOptions: { 
    mochaFile: "cypress/reports/junit/test-results.xml", 
    toConsole: true 
  } 
}); 
 

Run Tests with JUnit Reporting

npx cypress run --reporter mocha-junit-reporter

This will generate a test-results.xml file inside the cypress/reports/junit/ directory, which can be used in CI/CD dashboards.

5. Viewing Reports

  • Mochawesome HTML Report: Open cypress/reports/index.html in a browser.
  • JUnit XML Report: Use CI/CD tools to visualize results.

Advantages of Cypress

  • Fast Execution – Runs directly in the browser with no need for WebDriver.
  • Automatic Waiting – No need to add explicit waits; Cypress automatically waits for elements.
  • Easy Debugging – Provides real-time interactive debugging.
  • Network Control – Can stub API requests and responses.
  • Screenshots & Videos – Automatically captures screenshots and videos for failed tests.
  • Rich Documentation – Well-documented with active community support.

Disadvantages of Cypress

  • Limited Cross-Browser Support – Works well with Chrome, Edge, and Firefox, but lacks full Safari and IE support.
  • Only JavaScript Support – Cannot write tests in other languages like Java or Python.
  • No Multi-Tab Testing – Cannot handle multi-tab scenarios effectively.
  • Limited Mobile Testing – Works well for web applications but lacks built-in mobile testing support.
  • Runs in a Single Browser Context – Cannot switch between different browser contexts like Selenium.

Conclusion

Cypress is a powerful tool for automating document file testing in web applications. By using its built-in features and plugins, you can efficiently test file uploads and downloads, ensuring a seamless user experience.

This guide provides a structured approach to Cypress automation for document files. Implementing these strategies will help maintain the quality and reliability of your web applications.

Other BLOGS