What is Cypress and how does it differ from Selenium?
Cypress runs the test inside the browser, in the same run loop as the application, while Selenium drives the browser from outside through WebDriver. So Cypress sees the DOM directly, waits for elements itself through command retries and needs no sleep. The price: only JavaScript or TypeScript, a single browser tab, and what the browser cannot do is handled by a separate Node process.
Cypress · Architecture
A Selenium test fails with a stale element and Cypress does not. Why?
A team migrates a suite from Selenium to Cypress and notices something odd: the sleep(2000) calls between steps are gone and the flakiness dropped. There is no magic, there is a different architecture. This lesson explains where a Cypress test runs, how that differs from Selenium and what follows for writing tests.
How it works and why. Cypress is a framework for e2e and component testing of web apps in JavaScript or TypeScript. According to its documentation it executes in the same run loop as the application, and a Node process runs beside it for what the browser cannot do (files, screenshots, video, part of the network). Selenium, by contrast, drives the browser from outside: the test sends commands to a driver (the WebDriver protocol over HTTP), the driver controls the browser and returns a response.
Where the test runs
The decision to "live in the browser" solves concrete problems. The test has direct access to window, document, timers and the DOM, so Cypress sees when the page has loaded, when an element appeared and when a request finished, and can wait by itself. Before an action each command checks the element and retries until the timeout. Time travel follows from the same design: the Test Runner takes a DOM snapshot at every step and lets you go back to it. Under the hood Cypress uses Mocha for describe/it and hooks, Chai for assertions and Sinon for cy.stub, cy.spy and cy.clock.
Example: from sleep to automatic waiting. A typical Selenium test ported one to one, and its weak spot:
Selenium: sleep between steps
A 2-second pause is slow and on a slow CI still not enough. A found element can go stale if the DOM was redrawn between the lookup and the action.
Cypress: waiting inside the command
The command and the assertion are retried until the condition holds, and the element is queried from the DOM again on every attempt.
The same thing in Cypress has no pause at all:
it('login leads to the dashboard', () => {
cy.visit('/login');
cy.get('[data-cy=email]').type('qa@example.com');
cy.get('[data-cy=password]').type('secret');
cy.contains('button', 'Sign in').click();
// retried until it passes or the timeout ends
cy.url().should('include', '/dashboard');
});
Nothing waits explicitly: cy.get looks for the element until it appears, .click() waits until the element is clickable and should retries the check of the URL. The network calls the front end makes for data can also be watched with the built-in cy.intercept. That is why flakiness drops: the test does not guess the time, it describes the expected state.
Limits and alternatives. The same architecture brings limits. Tests are written only in JavaScript/TypeScript. The test lives inside one browser tab, so many tabs or windows at once are not available, and several domains in one test need cy.origin. Among supported browsers the documentation names the Chrome family (Chrome, Chromium, Edge) and Firefox; Safari itself is not supported (an experimental WebKit exists). Electron, still the default browser of cypress run, is deprecated as a test browser in Cypress 16 and will be removed in a future major version. If you need several users in different tabs, another language or real Safari, choose Playwright or Selenium.
Playwright also avoids WebDriver but works the opposite way to Cypress: it drives the browser from outside over one persistent connection and the browser's native protocol (CDP for Chromium, patched protocols for Firefox and WebKit). Tests can be written not only in JS/TS but in Python, Java and .NET.
Common mistakes.
- Adding
cy.wait(2000)"just in case", as a habitualsleep: the test gets slower and the flakiness stays. - Writing
await cy.get(...)or storing a command result in a variable as in ordinary async code: Cypress commands are queued and do not return a value right away. - Expecting several tabs or Safari from Cypress: that is a limit of its architecture, not a configuration mistake.
- Treating Cypress as "just Selenium with a better API": a different execution model means different approaches to waiting, network and data.
Check yourself
A colleague ports a Selenium test and leaves cy.wait(3000) after every click. The test is green. What is the problem and what should replace it?
Answer
The test passes but is slow and can still fail on a slow environment, because a fixed pause does not depend on the state of the app. Cypress already waits by itself: lookup commands and assertions are retried until the timeout. Replace the pause with an assertion on the expected state, for example cy.url().should(...) or cy.get(...).should('be.visible'), or wait for a request through an alias from cy.intercept.
Why does Cypress have direct DOM access and Selenium does not, and what is the price?
Answer
Cypress runs in the same run loop as the app, so it sees window and the DOM with no protocol between. The price: the test is tied to one browser tab and to JS/TS, several domains need cy.origin, and what the browser cannot do (files, a database) is done by a separate Node process through cy.task.