We have a new name! Browserbear is now Roborabbit

Cypress vs. Selenium: Which One is Better for Automated Testing

If you’re not sure of which automated testing tool to use, consider these two that are used by hundreds of thousands of developers around the world. In this article, we’ll compare Cypress and Selenium in terms of their cross-platform support, programming language support, and other features.
by Josephine Loo · May 2024

Contents

    Cypress and Selenium are two of the most popular choices for automated testing in the web development community. Each has its strengths and weaknesses, making them suitable for different use cases. In this article, we'll learn more about Cypress and Selenium, comparing their features to help you decide which one is right for you.

    What is Cypress

    Cypress is a comprehensive JavaScript component and end-to-end testing framework that is designed to simplify the process of setting up, writing, running, and debugging tests for developers and QA. It is specifically designed for testing modern web applications.

    The Cypress suite consists of two main products: the Cypress App, which is free and open-source, and the subscription-based Cypress Cloud. The Cypress App is the core of the Cypress suite, allowing developers to write and run all forms of tests, including unit, integration, and end-to-end for their web applications.

    The Cypress Cloud further enhances the capabilities of the Cypress App by providing seamless integration with Continuous Integration (CI) pipelines. It supports a wide range of CI providers, including CircleCI, GitHub Actions, Bitbucket, Jenkins, and more. This makes it an invaluable tool for maintaining the quality and reliability of web applications.

    What is Selenium

    Selenium is a widely used browser automation tool that has been around since 2004. It supports a wide range of web browsers and programming languages including non-mainstream ones like Haskell, Perl, Dart, and Pharo Smalltalk, making it a versatile choice for testing web applications.

    The Selenium suite is composed of Selenium WebDriver, Selenium IDE, and Selenium Grid. These components work together to facilitate automated testing across different browsers and environments, with Selenium WebDriver being the core of this automated testing ecosystem. With Selenium, you can automate interactions with web elements, such as clicking buttons, filling out forms, and navigating between pages.

    Being an open-source project, Selenium's growth is driven by community support. Contributions come from individual programmers, designers, QA engineers, and more. Additionally, companies like BrowserStack, Lambdatest, and Sauce Lab sponsor the Selenium project, further bolstering its development.

    Comparisons Between Cypress and Selenium

    Cross-Browser/Platform Support

    Cypress supports multiple browsers, such as Chrome, Electron, Firefox, and WebKit (experimental). Cypress is primarily focused on testing web applications in modern browsers on desktop platforms (Windows, macOS, Linux). It can’t be used to test native mobile apps but you can test the responsive mobile view of a web application by controlling the viewport with the cy.viewport() command.

    Selenium has an ecosystem full of WebDrivers that are created and maintained by third parties to support different internet browsers. Currently, browsers that are supported include Chrome, Firefox, Safari, Microsoft Edge, and Opera. Selenium WebDriver supports testing on desktop platforms (Windows, macOS, Linux) as well as mobile platforms (Android, iOS) through specialized drivers like Appium. This makes Selenium more versatile for cross-platform testing.

    Programming Language Support

    Cypress supports primarily JavaScript. As the Cypress test code is executed in the browser itself, JavaScript is all you need to write and run Cypress tests. Cypress also ships with official type declarations for Typescript. Hence, you can use Cypress with TypeScript 3.4+ too. While this limits the language choice, it also allows for more seamless integration with modern web development workflows that are built with JavaScript.

    The code below is a simple example from Cypress’s official guide to using Cypress (within the Cypress App) to perform automated testing in JavaScript. The last line of code checks whether the clicked URL is the expected URL that contains commands/actions:

    describe('My First Test', () => {
      it('clicking "type" navigates to a new url', () => {
        cy.visit('https://example.cypress.io')
    
        cy.contains('type').click()
    
        cy.url().should('include', '/commands/actions')
      })
    })
    

    Selenium supports multiple programming languages through Selenium drivers that are created and maintained by third parties, including Java, Javascript, PHP, Python, C#, R, Ruby, Go, Haskell, Perl, Dart, and Pharo Smalltalk. Each of these languages has its library that exposes commands from the Selenium API natively in the form of methods or functions.

    Here's an equivalent JavaScript code for the previous automated testing using Selenium:

    const { Builder, By } = require('selenium-webdriver');
    const assert = require('assert');
    
    const driver = new Builder().forBrowser('chrome').build();
    
    (async () => {
      try {
        await driver.get('https://example.cypress.io');
        
        const link = await driver.findElement(By.linkText('type'));
        await link.click();
        
        const currentUrl = await driver.getCurrentUrl();
        const keyword = '/commands/actions';
        assert.ok(await currentUrl.includes(keyword), `URL does not contain ${keyword}`);
        console.log(`URL contains ${keyword}`);
      } finally {
        await driver.quit();
      }
    })();
    

    Community Support

    Cypress's free and open-source app is MIT-licensed. Its GitHub repo has more than 616 contributors and is actively maintained. Besides that, there are various community events like webinars, meetups, and virtual activities for the community to connect and advance their testing skills with Cypress. As Cypress only supports JavaScript, the community is more focused, with a strong emphasis on modern web development practices and JavaScript.

    Selenium has been around for a long time. Its GitHub repo is used by 193k members and contributed by more than 670 developers. The community members of Selenium not only contribute code but also answer questions for other users in the user group, maintain and translate the website and documentation, report bugs, and request new features. Besides that, there are more resources, such as forums, blogs, and tutorials, available for Selenium users.

    Debugging

    Cypress is one of the automated testing tools with the best debugging features. When your test fails, there are hundreds of custom error messages that describe the exact reason for failure and you can debug them visually with its rich UI. When the test is running, you can see the command execution, assertions, network requests, page loads, and other events that are going on from the app and inspect the elements from the browser’s inspector. If you’re subscribed to Cypress Cloud, you’ll have access to even more detailed insights on your test's performance, like flaky test analytics, test suite analytics, failure and error summary, and more.

    Selenium does not have an integrated debugging interface like Cypress. Its debugging is done on the Selenium IDE. It is a browser extension that enables developers to develop test cases using Selenium most efficiently and debug them using built-in features like setting breakpoints and pausing on exceptions. It also records user actions in the browser when the tests are running.

    Parallel Execution

    Cypress has built-in support for parallel execution. It can run parallel tests across multiple virtual machines from version 3.1.0 onwards. Although parallel tests can be run on a single machine, it is not advisable to so do as this requires more resources to run the tests efficiently. Another thing to take note of is the run order of the tests is not guaranteed. The balance strategy will assign the spec file of each test to different virtual machines and start with the most time-consuming ones based on their previous run histories. This will help to minimize the overall test run duration.

    Selenium does not have built-in support for parallel execution. That said, you can run tests locally on any browser/OS combination in parallel using the Command-line Runner for Selenium IDE. It requires you to install the command-line runner, necessary browser drivers, and other dependencies before launching the runner from the command prompt with the options that you want. Alternatively, you can also run the tests in parallel across multiple remote machines using Selenium Grid.

    Other Features

    There are many other features that make Cypress and Selenium the top choices for automated testing, like:

    Cypress

    • Generates tests as you click and record each interaction with your application
    • Has an interactive selector playground that generates commands for matching any element
    • Automatically cancel a run after a configurable number of failures
    • Isolates the state of each test and clears the state of the browser before the next test runs (this reduces flaky test results)
    • Prioritizes the test specs that failed in the previous Cypress run automatically

    Selenium

    • Records multiple locators for each element it interacts with and retries other locators automatically when one fails
    • Allows reuse of test cases in multiple places throughout a test suite
    • Has an extensive control flow structure that allows you to add conditional logic and looping to your tests
    • Nesting control flow commands
    • Control flow syntax validation

    Conclusion

    Choosing between Cypress and Selenium depends on several factors, including your project requirements, team expertise, and specific testing needs. Generally, you should use Cypress if you prefer a JavaScript-centric approach that offers robust debugging capabilities and built-in support for parallel execution. On the other hand, if you need an automated testing tool that supports multiple programming languages and cross-platform testing as well as mobile platforms, go for Selenium instead.

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    Automate & Scale
    Your Web Scraping

    Roborabbit helps you get the data you need to run your business, with our nocode task builder and integrations

    Cypress vs. Selenium: Which One is Better for Automated Testing
    Cypress vs. Selenium: Which One is Better for Automated Testing