Cypress Tutorial

Welcome to Cypress Tutorial by TutorialKart.com.

Cypress is an end to end testing framework. It helps to perform front end testing on web applications with ease.

In this Cypress Tutorial, we will get a hands on experience with using Cypress framework to perform an end to end testing on a web application.

Cypress – Setup

You should have node.js installed prior to using Cypress. Node.js is a prerequisite. To install node.js on your computer, refer Node.js Tutorial.

Sample Web Application for this Cypress Tutorial

To demonstrate the working of Cypress framework, we shall consider a web application. The web application is a to do application. All the HTML, CSS and JavaScript is in the following file.

We shall try using Cypress framework to perform end to end testing on this application.

Create a HTML file with the following content in a directory.

Open this directory in a Visual Studio Code IDE and run the following command in a Terminal.

Run the following command to make the HTML page available at a URL.

Cypress Tutorial - Sample Web Application

Now the index.html will be available at http://127.0.0.1:8080/index.html.

Cypress – Create Project

Create a directory for placing your project. We shall use this directory as project folder in Visual Studio Code.

Open Visual Studio Code IDE.

Cypress Tutorial Step 1 - Open Visual Studio Code

Click on Open folder… and navigate to the directory we created for keeping project files.

Cypress Tutorial - Navigate to directory

The project folder would be empty, as we have not created any files in it.

Cypress Tutorial - Open Visual Studio IDE

Open Terminal window. From top menu click on Terminal -> New Terminal.

You can install Cypress using npm. Run the following command in the Terminal Window, that we just opened in Visual Studio Code.

Cypress Tutorial - Install Cypress

During Cypress installation, package.json file and node_modules directory are created in our project.

Run the following command, to create Cypress related file structure in the project.

Cypress project files would be created and a Cypress window would open as shown below.

Cypress Tutorial - Open Cypress Project Cypress Tutorial - Run Cypress

cypress/integration directory holds the test files. By default, examples folder is created with some sample test files. Delete the examples folder.

Now we are ready to start writing our first test file in Cypress.

Cypress – Writing Tests

Create a file named app.spec.ts in cypress/integration directory. Now, we shall write a simple test case in this file as shown below.

Cypress Tutorial - Write Tests

it() is used to define a test case. The first argument to is title for the test case, and the second argument is a function, that contains the logic to validate this test case.

cy.visit(url) loads the url.

cy.get(selector) traverses the html document loaded by the url and returns the nodes based on the argument to it. In this example, we have given the selector as “h1”, which means cy.get() gets all the h1 headings in the html document.

cy.contain(text) method gets the elements containing the text. The text is given as argument to it. Here, cy.get(‘h1’).contains(‘ToDo List’) filters all the elements returned by cy.get(‘h1’) based on the condition that the element should contain the text “ToDo List”.

should(‘be.visible’) asserts that atleast one of the elements should be visible.

All the cy methods we used in this example are chainable. Hence, we have written them as a single statement.

Cypress – Run Tests

To run Cypress tests, run the following command in terminal, from the root of your project folder.

We have already used this command to create the default folder structure and files for Cypress project.

After you run this command, Cypress window appears as shown below.

Cypress Tutorial - Run Tests

In the above screenshot, under INTEGRATION TESTS, we have only one entry ‘app.spec.js’. That is because, we have only one spec file in the integration folder. If we have multiple test files in this folder, all those files would appear as entries under INTEGRATION TESTS.

Now, you may click on Run all specs (present on the top right corner) to run all the test files or click on an entry to run that specific test file.

A new browser window appears and runs the specs with the default browser.

Cypress Tutorial - Our first Test Run

On the left hand side, you will see the individual test cases, along with test results, and on the right hand side you will see the actual steps being executed in real time in a browser.

You can write a test suite (multiple tests), using describe().

In the following app.spec.js, we have written following testcases.

  1. Check if the ToDo List page is loading.
  2. Check if we can Delete Item from ToDo List.
  3. Check if we are able to Add Item to ToDo List.

If you have closed the Cypress window, run the following command again. It will open Cypress window.

Run the app.spec.js file by clicking on it. The output would be as shown in the following screenshot.

Cypress Tutorial - Multiple Tests

Conclusion

In this Cypress Tutorial, we learned how to use Cypress to perform end to end testing on your web application.