Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

A Complete Cypress Tutorial: Learn Cypress From Scratch

This Cypress tutorial walks you through everything you need from setup to writing end-to-end Cypress tests along with real-world examples.

Published on:

  • Share:

If you're new to test automation or thinking about moving on from your current testing tools, this Cypress tutorial is a good place to start. Cypress is a JavaScript framework that makes it easy to test modern web applications. You can write tests quickly, debug them visually, and run them as part of your CI pipeline.

Overview

Cypress is a framework for automating web application tests in the browser. It supports end-to-end, integration, and unit testing with real-time reloading and debugging features.

Why Automate With Cypress Framework?

  • In-Browser Execution: Runs tests directly in the browser for accurate user simulation.
  • Live Feedback: View tests as they run, with instant visual updates.
  • Efficient Debugging: Use browser dev tools to quickly identify and fix issues.
  • CI Integration: Seamlessly integrates with Jenkins, GitHub Actions, and more.
  • Accessibility Checks: Automatically detect and resolve accessibility issues in each run.

How to Run Cypress Tests?

  • Install Cypress: Run npm install cypress --save-dev in your project directory.
  • Check Folder Structure: Ensure folders like "cypress" and files like "cypress.config.js" are generated.
  • Create Test File: Add the lambdatest_test_spec.cy.js file under cypress/e2e/lambdatest/ folder.
  • Write Your Test: Write your test scripts to validate the functionality of a web application.
  • Run in Headed Mode: Use npx cypress open to launch the Cypress Test Runner.
  • Select Test Runner Options: Choose E2E Testing and your preferred browser.
  • Execute Test: Click on the lambdatest_test_spec.cy.js file in the Cypress Runner to run the test.
  • Run in Headless Mode: Use npx cypress run to run tests without opening a browser.

What Is Cypress?

Cypress is an open-source, front-end testing framework to test modern web applications. It’s written in JavaScript and runs directly in the browser, allowing you to write unit, integration and end-to-end tests. It also comes with features like real-time reloads, time-travel debugging, and automatic waits which makes it easier to build reliable and maintainable tests.

Why Use Cypress?

Cypress can be used to write simple tests and complex tests as well. It also lets you write, run, and debug tests in the browser.

Features:

  • Run Tests Directly in the Browser: Cypress runs inside the browser, so you can test modern web apps exactly as users experience them.
  • View Tests in Real-Time: See your end-to-end and component tests run live as you develop, with instant feedback.
  • Debug With Built-In Browser Tools: Debug test failures using browser dev tools like console and DOM inspectors without leaving the test runner.
  • Integrate With CI: Easily integrate Cypress testing framework with CI tool like GitHub Actions, Jenkins using built-in support and Docker images.
  • Automate Accessibility Checks: Instantly visualize, triage, and fix accessibility issues on every tests.
Note

Note: Run Cypress tests on real browsers and OSes. Try LambdaTest Now!

How Cypress Test Execution Works?

This section of Cypress testing tutorial walks you through how Cypress works based on its core architecture.


Cypress Architecture By Sathwik Prabhu

By Sathwik Prabhu

  • Runs in the Browser Using Dual iFrames: Cypress framework injects two iFrames, one for your web application and one for the test runner directly into the browser. It allows Cypress to interact with your web application just like a user would, while still controlling the test environment.
  • Node.js Backend Powers System-Level Tasks: A local Node.js server is used to run the test. It manages browser launch, file access, screenshots, and supports the overall communication between the test runner and your web application.
  • Built-In Proxy Intercepts Network Requests: Cypress framework automatically routes all HTTP and HTTPS traffic through its proxy. This lets you easily stub or mock API responses, simulate failures, and control server behavior.
  • Real-Time Communication via WebSockets: The browser and Node.js backend stay connected through a WebSocket channel. It enables real-time updates and fast coordination between the web application and test execution.
  • Same Run Loop: Since both your web application and Cypress tests run in the same event loop, it reduces timing issues common in other test frameworks and makes tests more stable and reliable.

How to Run Cypress Tests Locally?

Cypress framework lets you write test scripts using JavaScript or TypeScript. It comes with Mocha (a popular JavaScript testing framework) built-in, so you don’t need to install Mocha separately.

Prerequisites

Install Cypress using the below command:


npm install cypress --save-dev

After installation, here is how the folder structure looks like:


CYPRESS_FOR_BEGINNERS/
├── cypress/
│   ├── e2e/
│   │   └── lambdatest/
│   │       └── lambdatest_test_spec.cy.js
│   ├── fixtures/
│   │   └── example.json
│   └── support/
├── node_modules/
├── base_reporter_config.json
├── cypress.config.js
├── lambdatest_run.json
├── lambdatest-config.json
├── package-lock.json
└── package.json

Writing First Cypress Test

In this section of Cypress tutorial, let's look at how to write your first Cypress test.

Step 1: Create a new lambdatest_test_spec.cy.js file under the cypress/e2e/ directory.

Step 2: Open lambdatest_test_spec.cy.js file and write your Cypress test.

The test script automates a user journey on the LambdaTest eCommerce Playground. It opens the login page, logs in with credentials, searches for a product named "VAIO," and verifies that "Sony VAIO" appears in the search results.


describe("Lambdatest Login ",() => {
 it("Open the URL", () => {
   cy.visit(
     "https://ecommerce-playground.lambdatest.io/index.php?route=account/login"
   );
 });
 it("Login into the application", () => {
   cy.get('[id="input-email"]').type("lambdatest@yopmail.com");
   cy.get('[id="input-password"]').type("lambdatest");
   cy.get('[type="submit"]').eq(0).click();
 });
 it("Search the Product", () => {
   cy.get('[name="search"]').eq(0).type("VAIO");
   cy.get('[type="submit"]').eq(0).click();
 });
 it("Verify Product after search ", () => {
   cy.contains("Sony VAIO");
 });
});

Running Cypress Tests on Local Grid

You can run Cypress tests using:

  • Headed Mode: Tests run in a visible browser window, allowing you to view each test step as it executes in real time.
  • Headless Mode: Tests run without opening a browser interface.

Run Tests in Headed Mode

You can run Cypress tests in headed mode using the Cypress App.

Step 1: Run the below command to execute Cypress tests locally in headed mode:


npx cypress open

After running the command, the below screen of Cypress App will launch. Select E2E Testing.


cypress app

Step 2: Choose your preferred browser, and open the Cypress Runner.


cypress runner

Step 3: Click on the lambdatest_test_spec.cy.js file to begin executing the tests. Once complete, you’ll see that all test steps have passed successfully.


Cypress App

Run Tests in Headless Mode

By default, Cypress runs in headless mode, so the tests execute in the background without launching a visible browser window.

Run the below command to execute Cypress tests locally in headless mode:


npx cypress run

You can notice in the screenshot below that all test cases were successfully executed in a headless browser.


cypress headless tests

How to Run Cypress Tests With LambdaTest?

So far in this Cypress testing tutorial, we’ve covered how to run Cypress tests locally. Now, let’s explore how to run tests with Cypress tool on a cloud-based grid.

Running Cypress tests in the cloud allows parallel execution at scale, reduces local environment dependency, and speeds up overall test cycles.

AI-native test execution platforms like LambdaTest let you run Cypress tests on a cloud grid. With its Cypress cloud, you can test across real browsers and OS combinations, enabling faster, scalable, and more reliable test execution.

To get started, refer to this guide on Cypress testing with LambdaTest.

...

To execute your Cypress tests on the cloud grid by LambdaTest, follow the steps below:

Step 1: Get your LambdaTest Username and an Access Key from your LambdaTest Account Settings > Password & Security.

Step 2: Install the LambdaTest Cypress CLI using the below command:


npm install -g lambdatest-cypress-cli

Step 3: In your project root directory, generate a sample config file by running the below command:


lambdatest-cypress init

This creates a lambdatest-config.json file where you’ll specify LambdaTest credentials, browsers and operating systems.

Step 4: Update the generated lambdatest-config.json file with your details. This configuration will run tests in parallel across Chrome, Electron, and Firefox on Windows 11.


{
  "lambdatest_auth": {
    "username": "your_username",
    "access_key": "your_access_key"
  },
  "browsers": [
    {
      "browser": "Chrome",
      "platform": "Windows 11",
      "versions": ["latest-1"]
    },
    {
      "browser": "Electron",
      "platform": "Windows 11",
      "versions": ["latest"]
    },
    {
      "browser": "Firefox",
      "platform": "Windows 11",
      "versions": ["latest-1"]
    }
  ],
  "run_settings": {
    "build_name": "Write First Script In Cypress",
    "parallels": 3,
    "specs": "./cypress/e2e/lambdatest/*.cy.js",
    "ignore_files": "",
    "network": true,
    "headless": false,
    "npm_dependencies": {
      "cypress": "13.6.2"
    }
  },
  "tunnel_settings": {
    "tunnel": false,
    "tunnel_name": null
  }
}

When writing this Cypress automation tutorial, the latest Cypress version is 14.4.1. We used v13.6.2 for our examples. However, you can go ahead with any newer version you're comfortable with.

Step 5: Once your configuration file is ready, run the tests using the below command:


lambdatest-cypress run --sync=true

Step 6: Visit the LambdaTest Web Automation dashboard to view your Cypress test results.


cypress headless tests

Best Practices for Cypress Testing

When performing Cypress UI testing, here are some of the best practices you can follow:

  • Log in Programmatically: Use cy.request() method to handle login via API instead of relying on the UI. It speeds up tests and avoids unnecessary steps.
  • Use Reliable Selectors: Target elements with data-cy or data-test attributes. These selectors are stable and won’t break with style changes.
  • Access Command Values With .then() Method: Cypress commands are asynchronous. Therefore, use .then() method to work with their returned values safely.
  • Keep Tests Independent: Each test should work in isolation without depending on the state or results of other tests. This improves reliability and debugging.
  • Group Related Assertions: Add multiple meaningful assertions in the same test to improve efficiency and reduce execution time.
  • Reset State Before Each Test: Ensure a clean starting point by resetting your web application state before running tests, not after.

Cypress Learning Resources

In addition to this Cypress automation tutorial, you can explore the resources below to further enhance your learning.

You can also go through these video-based Cypress tutorials to get started with test automation with Cypress framework.

Validate Your Skills with Cypress Certification

It is also important take your Cypress expertise to the next levels. And this is where Cypress 101 certification by LambdaTest offers you an excellent opportunity to validate and showcase your skills.

Benefits:

  • Get Certified for Free: Validate your end-to-end testing skills with free certification.
  • Hands-on Learning: Built around real-world scenarios to help you apply Cypress effectively in testing projects.
  • Skill Validation: Proves your ability to write reliable and maintainable Cypress tests.
  • Verified Badge: Earn a certification badge to showcase your Cypress expertise to peers and employers.
  • Career Growth: Strengthen your QA profile and stand out in job applications.

Conclusion

In this tutorial, you explored how to use Cypress tool to test modern web applications. You learned what Cypress is and how it runs directly in the browser. This Cypress tutorial for beginners also explained how to perform Cypress testing with examples. This covers testing on local grid in both headed and headless modes. You have also learned how to scale your tests with Cypress testing framework using the LambdaTest cloud grid along with some best practices.

Frequently Asked Questions (FAQs)

What is Cypress testing?
Cypress testing refers to using the Cypress framework to perform end-to-end testing of web applications. It tests how applications behave in a real browser by simulating user actions like clicks, typing, and navigation to ensure everything works as expected.
Is Cypress easy to learn?
Cypress is easy to learn, especially for JavaScript developers. Its simple syntax, real-time browser preview, and built-in features like automatic waits make writing, running, and debugging tests straightforward and efficient.
What is Cypress used for?
Cypress is used for end-to-end testing of modern web applications. It enables developers and QA engineers to write and execute tests in the browser, ensuring that user interactions, UI components, and workflows function correctly across various scenarios and updates.
Is Cypress better than Selenium?
Cypress is better than Selenium for fast, front-end testing of modern JavaScript applications due to its speed, simplicity, and real-time debugging. However, Selenium supports more browsers and languages, making it better for cross-browser and enterprise-level testing needs.
Which IDE is used for Cypress?
Cypress can be used with any JavaScript-friendly IDE. Visual Studio Code is the most popular due to its extensions and ease of use. WebStorm is another option with built-in support for JavaScript and end-to-end testing tools.
Can Cypress be integrated with CI/CD tools?
Yes, Cypress can be integrated with CI/CD tools like Jenkins, GitHub Actions, GitLab CI, CircleCI, and more. This allows automated test execution during code deployment, ensuring faster feedback, improved test coverage, and reliable continuous delivery pipelines.
What language does Cypress use?
Cypress uses JavaScript as its primary language. It also supports TypeScript, making it flexible for modern web development and testing environments.
Does Cypress support parallel test execution?
Yes, Cypress supports parallel test execution using its Dashboard Service. By configuring your CI pipeline and splitting tests across multiple machines or containers, you can speed up test runs and optimize overall test performance across your team.

Did you find this page helpful?

Helpful

NotHelpful

More Related Hubs

ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free