Improve Your Test Automation with Cypress

A road leading up to a large house in Tuscany with cypress trees lining each side of the road
Swathi Maanyam

SQA Engineer

Swathi Maanyam

Don’t settle for a partner who doesn’t care about your long-term goals. With DockYard we set you up for lasting succcess with everything from engineering support to project strategy and beyond. Book a free consult today to learn more.

Testing web applications can be a chore. Between clunky frameworks, unreliable tests, and debugging nightmares, it often feels like more trouble than it’s worth. But Cypress is an answer to that!

I discovered Cypress a while back, and it’s been a game-changer for my workflow. Cypress is a web-specific test automation framework that offers a user-friendly experience from writing tests to running and debugging them. It’s packed with features that make testing not only efficient but also enjoyable (yes, enjoyable!).

Streamlined Setup and Clean Tests

One of the things I love most about Cypress is how easy it is to set up. No more wrestling with complex configurations; Cypress integrates seamlessly with Node.js and its package managers, npm and yarn. Plus, the lifecycle hooks (before, beforeEach, after, and afterEach) make test setup and cleanup a breeze. No more worrying about tests interfering with each other – Cypress ensures each test runs in a clean and reliable state.

Another feature I find incredibly useful is the ability to manage test data using API endpoints. This keeps my testing environment pristine and avoids data conflicts.

Page Object Model (POM)

For anyone who’s ever battled with flaky UI tests, the Page Object Model (POM) is a lifesaver. Cypress fully embraces POM, allowing me to separate UI element selectors from my test logic. This is gold for complex UIs that require frequent testing. Imagine this: a UI element changes – instead of scrambling to update every test, I simply modify the relevant page object. It’s a win-win for maintainability and sanity!

Code Reusability

The commands.js file lets me store custom commands or helper functions, eliminating the need to write the same test logic over and over. Login, navigation, data manipulation, assertions – you name it, I can store it in commands.js. This not only saves me time but also reduces the risk of regressions in my tests.

Why Choose Cypress?

Here’s what truly sets Cypress apart:

Comprehensive testing: Cypress simulates real user interactions, testing the entire application flow. This holistic approach helps us catch integration issues before they become problems in production.

Automated testing: Cypress’ framework is specifically designed to be automated, meaning that test scenarios can be executed automatically without manual intervention. Integrating these tests into the CI pipeline ensures that they are triggered automatically whenever there is a change in the codebase.

Debugging tools: Cypress provides powerful built-in debugging tools. Features like cy.pause() allow developers to pause test execution and inspect the application state at a specific point, aiding in identifying and fixing issues more effectively.

Cross-browser testing: Cypress supports cross-browser testing, enabling teams to ensure their applications function consistently across different web browsers. This is essential for delivering a seamless user experience on various platforms.

Mocking and stubbing: Cypress allows for easy mocking and stubbing of network requests. This feature is valuable to control and manipulate the data used in tests without making actual network requests.

Command log: With the help of Command log, Cypress allows us to inspect the application’s state at specific points during test execution. This helps identify issues at different stages of an application

Synchronization: Cypress synchronizes tests with the application’s behavior, eliminating the need for manual waits and avoiding timing-related issues. This makes the testing process more reliable and less prone to errors.

Now, let’s get started by setting up Cypress in a new project and using the Cypress Test Runner to create and execute our first test.

Setting Up Cypress

Step 1: Install Node.js and npm

To use Cypress, we need to have Node.js installed on our system. Typically, Node.js comes with npm, the Node Package Manager. To download and install the latest version visit the official Node.js website (https://nodejs.org/en).

Note: We use npm for this tutorial, but you can also choose yarn.

Step 2: Create a New Project

Create a new directory for your project and navigate into it using your terminal:

mkdir my-cypress-project
cd my-cypress-project

Step 3: Initialize npm

Initialize npm in your project directory:

npm init -y

Step 4: Install Cypress

Install Cypress in your project:

npm install cypress

Step 5: Open Cypress Test Runner (Interactive mode)

In the terminal, run the following command. This will open Cypress in a new browser window, starting the Test Runner where you can create and run your tests.

npx cypress open

Step 6: Choose the testing type and browser

Select E2E Testing type. Choose a browser of your choice and click Start E2E testing.

Alt text

Alt text

Step 7: Create spec file

On the next screen, name your file and click Create new spec. Cypress by default creates an e2e folder for the spec files.

Alt text

Alt text

Step 8: Execute your spec files

Once a sample spec file has been created, you may close it or execute your file by clicking ‘Okay, run the spec’. If executed, the test is run in the browser chosen in step 6.

Alt text

To execute spec files in headless mode use below commands.

npx cypress run e2e/myFirstTest.cy.js

Additionally, we can create more scripts in the package.json file for faster execution.

Step 9: Editing your spec files

Spec files can be edited using TextEditor or other inbuilt tools but Visual Studio Code or JetBrains IDEs are preferred with Cypress for their built-in support. We can choose any preferred IDE. All spec files are located under the e2e folder.

For further reading, check out these resources:

Cypress Best Practices

Advanced Cypress Tips

Happy testing!

Newsletter

Stay in the Know

Get the latest news and insights on Elixir, Phoenix, machine learning, product strategy, and more—delivered straight to your inbox.

Narwin holding a press release sheet while opening the DockYard brand kit box