Modern GUI Testing with Cypress: An In-Depth Look

Kamil Sosnowski

Gone are the days when testing web applications was a time-consuming and tedious task. Automated GUI testing has emerged as a critical step in ensuring a smooth user experience in the world of modern web development. Among the numerous GUI testing tools available, Cypress has emerged as a strong contender, offering powerful capabilities that outperform competitors such as Selenium. In this blog post, we will look at the benefits of using Cypress for automated GUI testing as well as the features of its most recent version.

What is Cypress?

Cypress is an end-to-end testing framework designed specifically for web applications. It provides a comprehensive platform for developers to write and execute tests for their applications’ user interfaces. With its straightforward syntax, real-time reloading, and easy debugging capabilities, Cypress has quickly gained popularity among developers.

Cypress vs. Selenium: The Key Differences

  1. Architecture: One of the most significant differences between Cypress and Selenium lies in their architectural design. While Selenium uses a separate WebDriver server to interact with browsers, Cypress directly operates within the browser itself. This eliminates the need for additional layers of communication, resulting in faster and more reliable tests.
  2. Language Support: Selenium supports multiple programming languages, including Java, C#, and Python. However, this requires developers to maintain separate bindings and APIs for each language. On the other hand, Cypress is built on JavaScript, providing a single, unified testing experience.
  3. Real-time Reloading: Cypress’s real-time reloading feature is a game-changer. It automatically reloads tests as soon as changes are made to the test files, enabling faster feedback and shorter development cycles.
  4. Debugging: Debugging in Cypress is a breeze, thanks to its integration with the Chrome DevTools. Developers can easily inspect their tests and application code, making it simpler to identify and fix issues. In comparison, Selenium’s debugging capabilities are less user-friendly and often require additional tools or extensions.
  5. Test Flakiness: Cypress’s architectural design reduces test flakiness caused by timing issues and asynchronous behavior. It automatically waits for elements to become available and actions to complete before moving on, eliminating the need for manual timeouts and retries. This leads to more stable and consistent test results compared to Selenium.

Creating Your First Cypress Test and Running It

Now that you know the advantages of using Cypress for automated GUI testing, let’s walk through the process of creating and running your first example test. Follow these steps to get started with Cypress:

  • Install Cypress: To begin, ensure that you have Node.js installed on your system. Then, navigate to your project’s root directory and run the following command to install Cypress:
npm install cypress --save-dev
  • Initialize Cypress: Once Cypress is installed, initialize it by running the following command:
npx cypress open

This will create a “cypress” folder in your project directory, containing several example files and folders. The most important folders are:

“integration”: This is where your test files will be stored. “fixtures”: Contains static data for your tests. “plugins”: Stores Cypress plugins. “support”: Includes custom commands and reusable functions.

  • Write Your First Test: To create your first test, navigate to the “integration” folder and create a new file called “example_spec.js”. Open the file and enter the following code:
describe('My First Test', () => {
  it('Visits the example page', () => {
    cy.visit('https://example.com')

    cy.contains('Welcome to Example.com!')
    cy.title().should('include', 'Example Page')
  })
})

This test does the following:

  1. Visits the “https://example.com” URL.
  2. Checks if the page contains the text “Welcome to Example.com!”.
  3. Verifies that the page’s title includes “Example Page”.

The Page Object Factory improves the organization and maintainability of your test suite by centralizing the creation and initialization of page objects.

  • Run Your Test To run your test, execute the “npx cypress open” command in your project directory. This will open the Cypress Test Runner, which lists all your test files.

Click on “example_spec.js” to run your test. The Test Runner will execute the test in a new browser window, allowing you to observe the test execution and view the results.

Summary

Getting started with Cypress is a straightforward process, and creating your first test is as simple as following these steps. With its powerful features and user-friendly interface, Cypress is an excellent tool for automated GUI testing, ensuring that your web applications provide a smooth and reliable user experience. As you explore Cypress further, you’ll discover even more capabilities and techniques to enhance your testing process.

Meet the geek-tastic people, and allow us to amaze you with what it's like to work with j‑labs!

Contact us