Interview questions with answers
Cypress interview questions and answers
Commands, retry-ability, cy.intercept, fixtures and component tests. Open any question to read its model answer; the multiple-choice set marks the right option and explains it.
- Subjective
- 30
- Objective
- 30
Subjective questions 30
1How does Cypress work, and how is it different from Selenium?Asked almost every time
Cypress runs inside the browser alongside your app, controlled by a Node process, so it can read the DOM, stub network calls and control time directly. Commands are queued and run with automatic retries, and the runner shows each step with DOM snapshots (time travel). Selenium drives browsers from outside through WebDriver. Cypress is JavaScript-only and limited to one tab and one origin per test, though cy.origin handles cross-origin steps.
2Explain retry-ability.Asked almost every time
Queries such as cy.get and their assertions retry until the assertion passes or the timeout (4 s by default) expires, so tests wait naturally for the app. Actions such as .click() do not retry. Chain assertions directly after queries and avoid splitting them with non-retrying commands.
3Why are Cypress commands not promises, and how do you use values?Asked almost every time
Commands are enqueued and run later in order, so const el = cy.get('x') does not give you the element. Use .then(el => ...) to work with yielded values, aliases (.as('user') and cy.get('@user')), or assertions. Mixing async/await with Cypress commands does not work.
4How do you stub or wait for network requests?Asked almost every time
cy.intercept(method, url, response) stubs responses with fixtures or modifies requests; without a response it only spies. Give it an alias with .as('getUsers') and cy.wait('@getUsers') to wait for that call and assert on its request and response, instead of fixed waits.
5How do you structure a maintainable Cypress suite?Asked almost every time
Organise specs by feature, put shared actions in custom commands or small helper modules (App Actions rather than heavy page objects), seed data through API or cy.task, use data-cy selectors, keep tests independent and short, and run them in parallel in CI.
6What are common causes of flaky Cypress tests and their fixes?Asked almost every time
Fixed waits (cy.wait(5000)), asserting before network data arrives, detached DOM elements after re-renders, shared state between tests, and animations. Fix with assertions that retry, waiting on aliased requests, re-querying instead of storing elements, test isolation and disabling animations.
7How does Cypress handle asynchronous commands?Asked almost every time
Commands are enqueued when the test function runs and executed later, in order, by Cypress. That is why you cannot assign their results to variables; you chain with .then() or .should(), and Cypress waits and retries for you.
8What are the best practices for selecting elements?Very often asked
Use stable attributes dedicated to tests, such as data-cy or data-testid, or accessible text with cy.contains. Avoid brittle CSS classes, tag names and positional selectors that change with styling.
9How do you log in efficiently?Very often asked
Log in programmatically through the API (cy.request) rather than through the UI in every test, and cache the session with cy.session(id, setup), which saves cookies and storage and restores them in later tests. Test the login UI itself in one dedicated spec.
10What are custom commands?Very often asked
Reusable commands added with Cypress.Commands.add('login', (email) => { ... }) in the support file, so tests can call cy.login(email). Type them in a .d.ts for TypeScript. Keep them small and well-named.
11How do you keep tests independent and data predictable?Very often asked
Reset state before each test (seed the database through an API or cy.task), never depend on another test's results, stub third-party services, and use fixtures. Clean up in beforeEach, not afterEach, so a failed test cannot leave bad state behind.
12What is cy.origin used for?Very often asked
Running commands on a different origin within one test, for example a third-party login page. Code inside cy.origin(url, () => {...}) runs in that origin's context; you pass values in through the args option.
13How do you test file uploads and downloads in Cypress?Very often asked
Use cy.get('input[type=file]').selectFile('cypress/fixtures/file.pdf') (also supports drag-and-drop). For downloads, check the downloads folder with cy.readFile after the click.
14How do you handle authentication via API instead of the UI?Very often asked
Use cy.request to log in and receive a token or cookie, set it with cy.setCookie or localStorage, and wrap it in cy.session so it is cached and restored for every test, testing the UI login only once.
15What is test isolation in Cypress 12+?Very often asked
By default, Cypress clears the page, cookies, localStorage and sessionStorage before each test, so tests do not depend on each other. Use cy.session to restore login state explicitly.
16How do you test APIs with Cypress?Very often asked
Use cy.request to call endpoints and assert on status, headers and body. Plugins add nicer logging. It is handy for setup and smoke checks, though dedicated API test tools may suit large API suites.
17How do you parallelise Cypress in CI?Very often asked
Cypress Cloud balances specs across machines with --record --parallel. Without it, split specs manually or with third-party tools, and run in the official Docker images with dependencies installed.
18What is Cypress component testing?Often asked
Mounting individual React, Vue, Angular or Svelte components in a real browser with cy.mount, using your dev server's bundler, and testing them with the same Cypress commands. It sits between unit tests and full end-to-end tests.
19What does cy.task do?Often asked
It runs code in the Node process (defined in setupNodeEvents), for things the browser cannot do: seeding a database, reading files, or calling services. It returns a value back to the test.
20How do you control time in tests?Often asked
cy.clock() freezes Date, setTimeout and setInterval, and cy.tick(ms) moves time forward, so you can test countdowns or session timeouts without waiting.
21How do you run Cypress in CI?Often asked
Run cypress run headless, start the app first (for example with start-server-and-test), record or upload screenshots and videos as artifacts, and parallelise specs across machines with Cypress Cloud or your own splitting.
22How do you test accessibility with Cypress?Often asked
Use cypress-axe: cy.injectAxe() after visiting and cy.checkA11y() to report violations, plus keyboard checks such as focus order with cy.focused().
23What is the difference between should and then?Often asked
should retries its assertions until they pass or time out. then runs once with the yielded value and does not retry, so use it only when you need the value for further logic.
24How do you debug a failing Cypress test?Often asked
Use the Test Runner's time-travel snapshots, cy.pause() and .debug(), inspect yielded values in DevTools console, review screenshots and videos from CI, and add cy.log messages.
25How do you test responsive layouts?Often asked
Call cy.viewport('iphone-x') or cy.viewport(375, 812) in tests or config, and assert that mobile menus and layouts behave correctly.
26How do environment variables work in Cypress?Often asked
Set them in cypress.config env, in cypress.env.json, or as CYPRESS_* OS variables, and read them with Cypress.env('name'). Keep secrets out of the repository.
27How do you handle iframes in Cypress?Often asked
Cypress has limited native iframe support: get the iframe, wait for its body to load, then wrap it with cy.wrap(body) and query inside, or use a plugin such as cypress-iframe.
28What are fixtures?Sometimes asked
JSON or other files in cypress/fixtures loaded with cy.fixture('users.json') or referenced in cy.intercept(..., { fixture: 'users.json' }), giving consistent test data.
29How do you handle a new tab or window?Sometimes asked
Cypress controls one tab. Remove the target="_blank" attribute and click, or assert on the link's href and visit it directly.
30How do you write component tests with Cypress?Sometimes asked
Configure the component testing framework (React with Vite or webpack), mount components with cy.mount(<Button />), interact as in end-to-end tests, and stub props or network calls.
Objective questions 30
1Which command stubs a network response?Asked almost every time
Why: cy.intercept replaced the older cy.route.
2What is the default command timeout?Asked almost every time
Why: defaultCommandTimeout is 4000 ms.
3How do you use the element yielded by cy.get?Asked almost every time
Why: Commands are queued, not promises.
4Why can't you assign a Cypress command's result to a variable?Asked almost every time
Why: Use .then() or aliases.
5What fixes most timing flakiness?Asked almost every time
Why: Avoid fixed sleeps.
6What is cleared between tests by default since Cypress 12?Asked almost every time
Why: Test isolation.
7Which command caches a login between tests?Very often asked
Why: cy.session saves and restores session state.
8Which selector is most stable?Very often asked
Why: Dedicated test attributes do not change with styling.
9How do you wait for an aliased request?Very often asked
Why: Wait for the specific call, not a duration.
10Which actions do NOT retry?Very often asked
Why: Actions run once; queries retry.
11Which command runs steps on another origin?Very often asked
Why: Pass values with args.
12Which command uploads a file?Very often asked
Why: Built in since Cypress 9.3.
13Which retries: should or then?Very often asked
Why: then runs once.
14Which plugin adds accessibility checks?Very often asked
Why: cy.checkA11y.
15Which command sets the screen size?Very often asked
Why: Presets like iphone-x.
16Which command runs code in Node?Often asked
Why: Tasks are defined in setupNodeEvents.
17Which command mounts a component for testing?Often asked
Why: Used in component testing mode.
18Which commands control time?Often asked
Why: clock freezes timers; tick advances them.
19Where do reusable custom commands go?Often asked
Why: The support file loads before every spec.
20Which reads an environment variable in a test?Often asked
Why: From config or CYPRESS_ vars.
21Which pauses the test runner?Often asked
Why: Step through commands.
22Which command makes an HTTP request from the test?Often asked
Why: intercept stubs; request calls.
23Which flags balance specs across machines with Cypress Cloud?Often asked
Why: Requires a record key.
24Which command mounts a component in component tests?Often asked
Why: Framework-specific mount.
25Where do downloaded files go by default?Often asked
Why: Read them with cy.readFile.
26Which command runs Cypress headless?Sometimes asked
Why: open launches the interactive runner.
27Which loads test data from a file?Sometimes asked
Why: Fixtures live in cypress/fixtures.
28Which command handles steps on another origin?Sometimes asked
Why: cy.origin runs commands against a different origin.
29Which file can hold local Cypress env values?Sometimes asked
Why: Keep it out of Git.
30Cypress's native iframe support is…Sometimes asked
Why: Plugins help.
Track what you have practised and search every bank in your dashboard.
Start learning