Back to Test Automation
2026-03-205 min read

Install Cypress (Test Automation)

Learn Install Cypress (Test Automation) step by step with clear examples and exercises.

Title: Test Automation with Cypress (JavaScript) - Installation and Worked Example

Why This Matters

In today's fast-paced digital world, test automation is crucial for ensuring software quality and efficiency. Manual testing can be time-consuming, error-prone, and expensive. On the other hand, automated testing allows developers to run tests quickly, repeatedly, and consistently, saving valuable time and resources.

Cypress is a popular open-source end-to-end test automation tool that supports JavaScript. It's known for its simplicity, speed, real-time reloading, and user-friendly interface. In this lesson, we will guide you through the installation process of Cypress and provide a worked example to help you get started with test automation using JavaScript.

Prerequisites

To follow along with this lesson, you'll need:

  1. A basic understanding of JavaScript (ES6 syntax)
  2. Node.js installed on your system (version 14.x or later)
  3. npm (Node Package Manager), which comes bundled with Node.js

Core Concept

Cypress is a browser-based test runner that allows you to write, run, and debug tests for web applications. It supports both end-to-end (E2E) testing and component testing (CT). E2E tests simulate user interactions with the entire application, while CT focuses on individual components or features.

Cypress runs in a separate browser instance from your application, allowing it to interact with the DOM without affecting the running application. This makes it an excellent choice for test-driven development (TDD) and continuous integration (CI).

Installation

To install Cypress, you can use npm, Yarn, pnpm, or Bun:

Using npm

  1. Open your project root directory in a terminal.
  2. Run the following command to install Cypress as a dev dependency:
npm install cypress --save-dev

Using Yarn

  1. Open your project root directory in a terminal.
  2. Run the following command to install Cypress as a dev dependency:
yarn add cypress --dev

Using pnpm

  1. Open your project root directory in a terminal.
  2. Run the following command to install Cypress as a dev dependency:
pnpm add --save-dev cypress

Using Bun

  1. Open your project root directory in a terminal.
  2. Run the following command to install Cypress as a dev dependency:
bun add --dev cypress

Running Cypress

After installation, you can launch the Cypress App by running the following command:

Using npm

npx cypress open

Using Yarn

yarn cypress open

Using pnpm

pnpm cypress open

Using Bun

bunx cypress open

This will launch the Cypress App, where you can choose between E2E or CT and start writing tests.

Worked Example

Let's create a simple test for an example web application that has a form with two fields: a name input and a submit button. Our goal is to write a test that fills out the form with a name and submits it.

  1. First, navigate to the cypress/integration folder in your project root directory. This is where Cypress looks for test files by default.
  2. Create a new file named example_test.js.
  3. Open the file and write the following code:
describe('Example Test', () => {
it('Fills out and submits the form', () => {
cy.visit('http://your-example-app-url'); // Replace with your app's URL

cy.get('#nameInput').type('John Doe'); // Assuming the name input has an id of 'nameInput'
cy.get('#submitButton').click(); // Assuming the submit button has an id of 'submitButton'

cy.url().should('include', '/submitted'); // Assuming the URL changes after submission
});
});
  1. Save the file and run the test by clicking on the "Run" button in the Cypress App. If everything is set up correctly, the test should pass, demonstrating that we can write simple tests with Cypress using JavaScript.

Common Mistakes

  1. Forgetting to import necessary modules: Ensure you have imported cypress at the beginning of your test file:
import { describe, it } from 'mocha';
import { expect } from 'chai';
import { mount } from '@cypress/react'; // If using React
import './styles.css'; // If you have a separate CSS file
import App from './App'; // The component to test

describe('Example Test', () => {
it('Fills out and submits the form', function() {
// Your test code here
});
});
  1. Not waiting for elements to load: Sometimes, elements may not be immediately available when the page loads. In such cases, use Cypress commands like cy.get(), cy.wait(), or cy.contains() to wait for the element to appear before interacting with it:
cy.get('#nameInput').should('be.visible'); // Waits until the input is visible before continuing
cy.get('#nameInput').type('John Doe');
  1. Not handling asynchronous actions: If your application uses asynchronous functions, you may need to use cy.wrap() or cy.task() to handle them correctly:
cy.wrap(async () => {
const result = await someAsyncFunction();
// Continue with your test code
});

Practice Questions

  1. Write a test for a form that has an email input and a submit button. The test should fill out the email input with 'test@example.com' and click the submit button.
  2. Modify the example test to handle a case where the form submission fails due to invalid data (e.g., an empty name).
  3. Write a test for a login page that has a username input, a password input, and a login button. The test should fill out the inputs with valid credentials and click the login button, then verify that the user is successfully logged in by checking for a specific page or element.

FAQ

  1. Why does my test fail when I run it outside of the Cypress App?
  • Make sure you have installed Cypress as a dev dependency and are running your tests using the command provided in the "Running Cypress" section.
  1. How can I write tests for a React application with Cypress?
  • You can use the @cypress/react package to mount your React components within a Cypress test:
import { mount } from '@cypress/react';
import App from './App';

mount(<App />);
  1. How do I handle asynchronous functions in my tests?
  • Use cy.wrap() or cy.task() to handle asynchronous actions:
cy.wrap(async () => {
const result = await someAsyncFunction();
// Continue with your test code
});
  1. Why is my test failing when I try to interact with an element that doesn't exist yet?
  • Use Cypress commands like cy.get(), cy.wait(), or cy.contains() to wait for the element to appear before interacting with it:
cy.get('#nameInput').should('be.visible'); // Waits until the input is visible before continuing
cy.get('#nameInput').type('John Doe');
Install Cypress (Test Automation) | Test Automation | XQA Learn