How to configure Cypress for comprehensive browser testing

Key takeaways:

  • Cypress supports unit, integration, and end-to-end testing in a single framework.

  • Use cypress.json to customize settings like baseUrl, viewport sizes, and test file locations.

  • Write tests using Mocha and Chai syntax in the cypress/integration directory, with support for JavaScript or TypeScript.

  • Utilize the Test Runner for real-time test execution and logs; supports headless mode for CI environments.

Cypress is a powerful tool for browser testing that goes beyond traditional end-to-end testing. Its robust features allow developers to conduct a wide range of tests, including unit, integration, and end-to-end tests, all within a single framework. This guide provides a detailed walkthrough of setting up Cypress for browser testing, covering installation, configuration, and best practices.

Installation

  1. Cypress can be easily installed via npm using the following command:

npm install cypress --save-dev
  1. Once installed, Cypress can be initialized in the project directory using:

npx cypress open

Upon execution of this command, the Cypress GUI will launch as follows:

Cypress GUI
Cypress GUI

Configuration

Cypress has a default configuration file (cypress.json) where settings can be customized.

Key configuration options include:

  • baseUrl: Set the base URL for your application under test.

  • viewportWidth and viewportHeight: Define the default viewport size for tests.

  • testFiles and integrationFolder: Configure the location of test files.

  • video and screenshots: Enable/disable video recording and screenshots during test runs.

Cypress also supports environment variables for configuration flexibility.

Writing tests

Tests in Cypress are written using MochaMocha is the underlying testing framework used for organizing and executing test cases. It provides features like hooks, test suites, and assertions, making it easy to structure tests. Cypress integrates Mocha, allowing users to write tests using Mocha's syntax alongside Cypress's browser automation capabilities. and Chai Chai is often used alongside Mocha as the assertion library. Cypress itself provides a default assertion library, but users can also choose to use Chai for assertions if they prefer its syntax and features. With Chai, developers can write assertions in a variety of styles, including BDD (Behavior-Driven Development) and TDD (Test-Driven Development), making it versatile for different testing approaches. Chai offers a rich set of assertion methods for various data types and scenarios, helping developers write more descriptive and understandable test cases.syntax, providing a familiar testing experience for developers. Test files are typically organized within the cypress/integration directory. Cypress provides a powerful API for interacting with elements on the page, making it easy to simulate user actions. Test files can be written in JavaScript or TypeScript, depending on the project’s requirements.

Running tests

  • Cypress provides a user-friendly Test Runner interface for executing tests.

  • Tests can be run interactively using the Test Runner, which displays test results in real time.

  • Cypress also supports headless mode for running tests in continuous integration (CI) environments.

  • Test runs generate detailed logs, including command output, assertions, and screenshots/videos for failed tests.

Best practices

  • Write focused and independent tests to ensure reliability and maintainability.

  • Leverage Cypress commands and utilities for efficient test creation.

  • Use aliases and custom commands to simplify test code and improve readability.

  • Implement robust error handling and retries for flaky tests.

  • Regularly review and refactor test code to keep it maintainable over time.

Sample test scenario with Cypres

To create a basic test spec for Cypress, we can use a set of predefined scenarios that come with the basic installation. Many of them use the website as a target to learn and ramp up with the frameworks and their APIs.

To perform Cypress testing, we need to follow the steps below:

Step 1: Click the "Continue" button
Step 1: Click the "Continue" button
1 of 5

Let’s look at a basic test code that we can use. Click the “Run” button and follow the steps above to perform E2E testing on Cypress:

describe('Sample Test Suite', () => {
  it('Visits the Example website', () => {
    // Visit the Example website
    cy.visit('https://example.com');

    // Assert that the page title contains 'Example Domain'
    cy.title().should('contain', 'Example Domain');
  });

  it('Searches for a term', () => {
    // Visit the Google website
    cy.visit('https://www.google.com');

    // Type a search term into the search input field
    cy.get('[name="q"]').type('Cypress testing');

    // Press the Enter key to perform the search
    cy.get('[name="q"]').type('{enter}');

    // Assert that the search results contain the term 'Cypress testing'
    cy.contains('Cypress testing');
  });
});
Cypress Test: Page navigation and search verification

In the above code:

  • Line 1: We are grouping related tests together into a “test.” Think of a test suite as a collection of tests that focus on testing a specific feature or functionality.

  • Line 2: We are defining a specific test within our test suite. This test is named "Visits the Example website" and will check if Cypress can successfully open the Example website.

  • Line 4: We are telling Cypress to go to a specific website. We are using the cy.visit() command, which is like telling Cypress to open a web browser and go to the provided URL.

  • Line 7: Checks if the title of the web page we just visited contains the words "Example Domain". If it does, our test passes; if it doesn’t, our test fails.

  • Line 10: We are starting a new test within the same test suite. This test will check if Cypress can perform a search on the Google website.

  • Line 12: We are telling Cypress to open a web browser and go to the Google website.

  • Line 15: We are finding the search bar on the Google homepage and typing the words "Cypress testing" into it.

  • Line 18: After typing the search term, we are simulating pressing the “Enter” key on the keyboard to actually perform the search.

  • Line 21: Finally, we are checking if the search results page contains the words "Cypress testing". If it does, our test passes; if it doesn’t, our test fails.

Conclusion

Setting up Cypress for browser testing is a straightforward process that offers immense benefits in terms of test coverage and reliability. By following the steps outlined in this guide and adhering to best practices, developers can harness the full power of Cypress to ensure the quality and stability of their web applications.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to set browser for test in Cypress

You can set the browser for Cypress tests by using the --browser flag when running tests. For example: npx cypress open --browser chrome will open Cypress in Chrome.


Can Cypress do cross browser testing?

Cypress supports cross-browser testing with browsers like Chrome, Edge, and Electron. However, it currently does not support browsers like Firefox to the same extent.


How do I change the default browser in Cypress?

To change the default browser in Cypress, you can specify the desired browser using the cypress open or cypress run commands with the --browser flag, or set the default browser in your Cypress configuration file.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved