Cypress Interview Questions
Cypress interview questions — architecture, retries, intercept, sessions and Cypress vs Selenium — with answers.
Cypress interview questions
Cypress interviews focus on how Cypress differs from Selenium, its asynchronous command queue, retries and network control.
1. How is Cypress different from Selenium?
Selenium drives the browser from outside through the WebDriver protocol and supports many languages. Cypress runs inside the browser with the app, in JavaScript/TypeScript, with automatic waiting, time-travel debugging and built-in network stubbing. Cypress has limited multi-tab and older multi-origin support; Selenium supports more browsers and languages.
2. Why can't you assign cy.get() to a variable and use it later?
Cypress commands are asynchronous and queued — cy.get() returns a chainable, not the element. Chain commands, or use .then() to access the yielded value.
3. What is retry-ability?
Queries (get, find, contains) and their assertions are retried until the assertion passes or the timeout expires (4 s by default). Actions like click and type are not retried.
4. What's the difference between .should() and expect()?
.should() is chained to a command and retries. expect() inside .then() runs once against a fixed value.
5. What is cy.intercept() used for?
Spying on network requests (to wait for them or assert on their payload) and stubbing responses (to test error, empty and loading states deterministically).
6. How do you wait for an API call instead of using cy.wait(5000)?
Register cy.intercept('GET', '/api/items').as('items') before the action, then cy.wait('@items').
7. What are fixtures?
Static test data files in cypress/fixtures/, loaded with cy.fixture() or used directly as stubbed responses: cy.intercept(..., { fixture: 'items.json' }).
8. What are custom commands?
Reusable commands added with Cypress.Commands.add() in cypress/support/commands.js, e.g. cy.login().
9. How do you avoid logging in through the UI in every test?
Log in with cy.request() and cache it with cy.session(), so later tests restore cookies and storage instantly. Test the login page itself separately.
10. Which selectors does Cypress recommend?
Dedicated test attributes such as data-cy or data-test, because they don't change with styling or copy.
11. How do you handle secrets like passwords?
Use Cypress.env() with values passed via --env or CYPRESS_* environment variables (CI secrets), never committed in the repo. Use { log: false } when typing them.
12. cypress open vs cypress run?
open launches the interactive Test Runner for writing and debugging. run executes tests headlessly, for CI, saving screenshots (and videos if enabled).
13. How do retries work in Cypress?
Configure retries: { runMode: 2, openMode: 0 } globally or per test. A failed test is re-run before being marked failed. Use for rare infrastructure issues, not to hide flaky tests.
14. How do you test file uploads?
cy.get('input[type=file]').selectFile('cypress/fixtures/report.pdf') (built into Cypress 9.3+), then assert on the UI or the intercepted upload request.
15. What are before, beforeEach, after and afterEach?
Mocha hooks that run once before all tests, before each test, after all tests and after each test in a describe block. Use beforeEach to keep tests independent.
16. Why should tests not depend on each other?
Cypress (and CI parallelisation) can run them in any order or in isolation; dependent tests fail randomly and are hard to debug. Each test should set up its own state.
17. How do you run Cypress in CI?
cypress run headlessly — typically with cypress-io/github-action, which installs dependencies, starts the app, waits for it and uploads screenshots/videos as artifacts.
18. When would you choose Playwright over Cypress?
When you need multiple tabs or origins, WebKit/Safari coverage, multiple languages (Python, Java, .NET) or parallel runs without a paid service. Cypress remains excellent for front-end teams who value its interactive debugging.