Cypress Web Testing Framework: Getting Started

August 2024 · 4 minute read

Cypress has been developed to introduce developers to software testing and built for the modern web and to address the pain points of QA teams while testing a web application.

It supports different types of testing:

If you’re new to Cypress web testing, let’s learn about the features with the help of examples and scenarios for running your first Cypress test.

Table of Contents

What is Cypress Framework?

Cypress framework is a JavaScript-based end-to-end testing framework built on Mocha – a feature-rich JavaScript test framework running on and in the browser, making asynchronous testing simple and convenient. Cypress testing library also uses a BDD/TDD assertion library and a browser to pair with any JavaScript testing framework.

Cypress Web Testing Framework: Getting Started

Features of Cypress Framework

Two primary features explicitly developed for Cypress web testing are:

Advantages of Cypress Web Testing

Installing Cypress Web Testing Framework

Cypress can be installed using Npm package manager as shown below:

npm install cypress --save-dev

This command will download and configure Cypress on the system.

Components of Cypress Framework

The following components play a part while executing particular test cases:

Now let’s take a look at the workflow of the Cypress Web Testing Framework.

Workflow

  • Open the application to write the test case.
  • Identify the locators/selectors required for the particular flow of that test. Add those to the Page Selector file.
  • Add test steps in Cypress syntax as commands under the Page Object File methods.
  • Invoke the page object method in the actual Spec File.
  • There are numerous ways to execute Cypress tests. If it is executed as part of the project structure, one can use the ng tool:
  • Execute the test suite with ng e2e command.
  • For executing a specific test, run: ng e2e projectName –spec=Spec File Path
  • How to run your first Cypress Test? (With Example)

    1. As mentioned earlier, npm has been used to install Cypress. Consequently, it has now been installed to the ./node_modules directory, with its binary executable accessible from ./node_modules/.bin.

     2. Now, open Cypress from the project root via one of the following ways:

    The long way with the full path

    ./node_modules/.bin/cypress open

    Or with the shortcut using npm bin

    $(npm bin)/cypress open

    3. Adding npm scripts

    It is easier to add Cypress commands to the scripts field in the package.json file:

    { "scripts": { "cypress:open": "cypress open" } }

    Now invoke the command from the project root with:

    npm run cypress:open

    Or

    Also, use npx to invoke the command as shown below:

    npx cypress open

    4. Once Cypress is open, write the script and work on the test case:

    5. Create a new file in the cypress/create folder:

    touch {your_project}/cypress/integration/ex_spec.js

    6. Once the file has been created, one should see the Cypress Test Runner immediately being displayed in the Integration Test list.

    7. Open an IDE and add the code below to the ex_spec.js test file:

    describe('First Test', () => { it('Welcome!', () => { expect(true).to.equal(true) }) })

    8. Once this file is saved, the user should see the browser reload.

    And that is how a user can run their first Cypress script.

    Remember that Cypress testing must be executed on real browsers for accurate results. Start running web automation tests on 30+ versions of the latest browsers across Windows and macOS with BrowserStack. Use instant, hassle-free parallelization to get faster results without compromising on accuracy. Detect bugs before users do by testing software in real user conditions with BrowserStack.

    Try Cypress Testing for Free

    ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6csKmqlajAbrLRmqSer5%2BnuG7A1K2mq6GRoQ%3D%3D