Cypress vs Selenium: Key Differences
By Jash Unadkat, Technical Content Writer at BrowserStack - February 14, 2023
Test Automation is pivotal for the software development process to deliver excellence in a shorter release cycle. Hence, choosing the right Test Automation framework like Selenium or Cypress becomes critical for the process. Selenium vs Cypress helps you identify when to use which Automation framework to meet all the testing requirements.
Let’s begin with the most fundamental question.
Why compare Cypress and Selenium?
Cypress and Selenium are test automation tools used for functional testing of web applications by automating browser actions. Selenium has been a widely-used tool for years, whereas Cypress is a recently introduced tool in the test community.
In spite of Cypress being recently introduced, it has gained significant traction. With such significant growth in the adoption of Cypress, it’s natural for testers to compare the two before choosing the ideal tool for test automation.
So, the question is, how does Cypress compare to Selenium? Is Cypress a better alternative to Selenium?
An easy way to answer these questions is to evaluate the advantages and limitations of each tool.
What is Cypress?
The official documentation states that Cypress differs fundamentally and architecturally as compared to Selenium.
Using Cypress, QAs or developers can create:
- Unit tests
- Integration tests
- End to End tests
The architectural improvements in Cypress empower testers with the ability to perform Test-Driven Development (TDD) with complete end to end testing. Cypress is developed with the intent to make development and testing a parallel process.
Note: Cypress scripts cannot be executed outside the browser like in Selenium. All Cypress test scripts are executed within the browser. For example, to click on a specific button, Cypress doesn’t send the command to the browser using a specific driver. Instead, it uses the DOM events to send the click command to the button. This results in a much faster execution of test results.
Advantages of Cypress
- Cypress framework captures snapshots at the time of test execution. This allows QAs or developers to hover over a specific command in the Command Log to see exactly what happened at that particular step.
- One doesn’t need to add explicit or implicit wait commands in test scripts, unlike Selenium. Cypress waits automatically for commands and assertions.
- Developers or QAs can use Spies, Stubs, and Clocks to verify and control the behavior of server responses, functions, or timers.
- The automatic scrolling operation ensures that an element is in view before performing any action (for example Clicking on a button)
- Earlier Cypress supported only Chrome testing. However, with recent updates, Cypress now provides support for Firefox and Edge browsers.
- As the programmer writes commands, Cypress executes them in real-time, providing visual feedback as they run.
Cypress carries excellent documentation.
Limitations of Cypress
- One cannot use Cypress to drive two browsers at the same time
- It doesn’t provide support for multi-tabs
- Cypress doesn’t provide support for browsers like Safari and IE at the moment.
- Limited support for iFrames
People also read: Getting Started with Cypress Installation for Test Automation
Now, let’s explore Selenium.
What is Selenium?
As mentioned earlier, Selenium is a popular test automation tool that automates web-browsers. This open-source tool has been a leading choice for testers for over a decade now.
It allows QAs to automate test cases for the desired browser by using the Selenium WebDriver library along with a language-specific framework. Download the browser-specific driver before getting started with browser automation.
Developers and QAs also have the flexibility to choose the programming language of their choice. Selenium developers have developed language bindings for multiple languages like Ruby, Python, Java, etc.
WebDriver uses the JSON wire protocol for executing test cases. At a high level, execution involves three main steps:
- Test commands are translated into a URL
- Browser Drivers receive these URLs using the HTTP server
- The URLs are forwarded as a request to the actual browsers, and all commands in the test scripts are executed.
Refer to this detailed explanation of Selenium WebDriver’s architecture for a better understanding of this operation.
Key Advantages of Selenium
- Compatible with multiple OS like Windows, Linux, Unix, Mac
- Provides QAs the flexibility to select the programming language of their choice like Java, Ruby, Python, etc.
- Compatible with modern browsers like Safari, Chrome, Firefox, etc.
- Provides Concise APIs
Limitations of Selenium
- No built-in command for automatic generation of test results
- Handling page load or element load is difficult
- Limited support for testing images
- Creating test cases is time-consuming
- Difficult to set up test environment as compared to Cypress
People also read: How to Build and Execute Selenium Projects
Cypress vs Selenium: Core Differences
|Supports all popular languages like Java, Python, Ruby, C#, Php, etc.
|Chrome, Edge, Firefox, Electron
|Chrome, IE, Safari, Edge, Firefox, Opera
|Supports only Mocha JS
|The setup is simple. No dependencies or additional downloads required
|Setup is a bit challenging as it requires downloading browser-specific drivers and setting up the test environment
|Documentation & Community Support
|Very intuitive documentation along with a rapidly growing community
|Well-established documentation and firm community support from users across the globe
Selenium vs Cypress: When to use which for Test Automation
Although Selenium and Cypress are both designed to automate browsers for testing purposes, they differ considerably in terms of architecture and performance. A key difference is that Cypress as a tool is ideal for introducing developers to test automation rather than just a replacement for Selenium. This is why Cypress is among the fastest-growing automation tools in the world. On the other hand, Selenium is a more general-purpose tool targeted at a broader audience.
Cypress is preferred over Selenium when one need:
- Single Framework for Component, API, End to End, Visual, Accessibility, Performance testing
- Video recording capability of test execution
- Out of the box retry capability of actions performed over elements, which reduces flaky test for Cypress
- Single Programming language across development (Front end) and Automation
- Capability to Mock/Stub Request and Responses during early stage of development
- Save time building framework from Scratch with Selenium, as Cypress provides stable and ready to use Framework with everything wrapped and ready to use functions
- Test Runner which provides time travel capability to go through individual step with before and after screenshots attached to debug failures
- Meaningful exceptions when any test fails
Read More: How to handle Errors in Cypress
Selenium is preferred over Cypress when one need:
- When you need to run a test case on different browsers simultaneously then Selenium Grid works the best, since Cypress cannot be used to drive two browsers at the same time.
- Cross browser testing at scale becomes easy with Selenium. Cypress does not support browsers like Safari, which restricts the cross browser testing support, when compared to Selenium.
- Selenium supports NUnit and JUnit that are not supported by
Needless to say, prior to choosing an automation tool, one must weigh the pros and cons of every option. This will help evaluate which tool works best to serve the team and organization in the long term. No matter which test automation framework you choose for your web application, it is essential to test the web application on real devices and browsers for more accurate result.
Running your Cypress or Selenium tests on BrowserStack’s Real Device Cloud helps you get access to 3000+ real device and browser combinations for maximum test coverage. It allows you to test under real user conditions, which will help identify the bottlenecks in the real user experience and rectify them. BrowserStack’s Cloud Selenium Grid allows you to run cross browser tests on different browsers and devices simultaneously by leveraging parallel testing for faster testing with a vast coverage.