Cypress Junior cheat sheet: interview answers

Every question of this level with its correct answer and explanation, on one page for a quick review before the interview.

  1. 1. Where do Cypress tests run?

    Correct answer: Inside the browser, next to the app under test

    Cypress runs test code in the same browser run loop as the app, with a Node.js process alongside for tasks like files and network proxying. That's why it can read app state directly.

  2. 2. Which command opens the interactive Cypress app?

    Correct answer: npx cypress open

    cypress open launches the app where you pick a browser and spec and watch tests run. cypress run runs tests headlessly, as in CI.

  3. 3. Which command runs all Cypress tests headlessly, e.g. in CI?

    Correct answer: npx cypress run

    cypress run runs every spec in the terminal, records failure screenshots, and returns a non-zero exit code on failures.

  4. 4. Where is the main Cypress configuration usually defined?

    Correct answer: In cypress.config.ts or cypress.config.js

    The config file sets baseUrl, spec patterns, viewport, timeouts, retries and Node event handlers via defineConfig.

  5. 5. What does setting baseUrl in the Cypress config allow?

    Correct answer: Writing cy.visit('/login') with relative paths

    A base URL also makes Cypress check the server is up before the run, and it's easy to change per environment.

  6. 6. What is a spec file in Cypress?

    Correct answer: A test file, e.g. login.cy.ts, containing tests

    By default end-to-end specs live in cypress/e2e and match *.cy.{js,ts}. Each spec runs in a fresh browser context.

  7. 7. Which functions structure Cypress tests?

    Correct answer: describe and it from Mocha

    Cypress bundles Mocha for structure and Chai for assertions, plus hooks like beforeEach.

  8. 8. What does cy.visit('/products') do?

    Correct answer: Opens that page in the test browser and waits for it to load

    cy.visit waits for the page's load event before moving on. Use cy.request to call an API without rendering.

  9. 9. What does cy.get('[data-cy=submit]') do?

    Correct answer: Finds elements by selector, retrying until they appear

    cy.get queries the DOM and retries until the element exists or the timeout (4 s by default) runs out.

  10. 10. Why does Cypress recommend data-cy or data-test attributes for selectors?

    Correct answer: They're stable and not tied to styling or text

    Classes change with design, text changes with copy. Dedicated test attributes survive both and show the element is used by tests.

  11. 11. What does cy.contains('Save') find?

    Correct answer: An element containing the text "Save"

    cy.contains searches by text, optionally within a selector: cy.contains('button', 'Save'). It retries like cy.get.

  12. 12. How do you type into an input in Cypress?

    Correct answer: cy.get('#email').type('a@b.co')

    .type() sends key events one by one. Special keys use braces, like {enter}. To clear first, chain .clear().

  13. 13. How do you press Enter after typing?

    Correct answer: .type('query{enter}')

    Special sequences like {enter}, {esc} and {selectall} go inside .type().

  14. 14. How do you select an option in a native <select>?

    Correct answer: cy.get('select').select('Blue')

    .select() accepts the option's text, value or index. Custom dropdowns are tested with clicks.

  15. 15. How do you tick a checkbox in Cypress?

    Correct answer: cy.get('[type=checkbox]').check()

    .check() and .uncheck() work for checkboxes and radios and can take values to pick specific ones.

  16. 16. What does Cypress check before clicking an element?

    Correct answer: It's visible, not disabled, not covered and not animating

    These actionability checks are retried. If the element stays covered, the click fails with a clear message.

  17. 17. What does { force: true } on a click do, and why is it risky?

    Correct answer: Skips actionability checks, which can hide real UI bugs

    If a modal covers a button, a real user can't click it. Forcing the click makes the test pass anyway.

  18. 18. What does .should('be.visible') do?

    Correct answer: Asserts visibility, retrying until it passes or times out

    Assertions chained with .should() make Cypress retry the previous query, which handles content that appears later.

  19. 19. Which assertion checks an input's value?

    Correct answer: .should('have.value', 'John')

    Input values aren't text content. have.value reads the current value; have.text reads text nodes.

  20. 20. How do you check a list has exactly 3 items?

    Correct answer: cy.get('li').should('have.length', 3)

    have.length retries until the count matches, so it works for lists that render gradually.

  21. 21. How do you assert an element is NOT on the page?

    Correct answer: .should('not.exist')

    not.exist passes when the query finds nothing. not.be.visible is different: the element exists but is hidden.

  22. 22. Which assertion checks the current URL contains /dashboard?

    Correct answer: cy.url().should('include', '/dashboard')

    cy.url() yields the URL and retries with the assertion. cy.location('pathname') yields just the path.

  23. 23. What is the default timeout for most Cypress queries like cy.get?

    Correct answer: 4 seconds

    defaultCommandTimeout is 4000 ms. Page loads have a longer pageLoadTimeout; both can be changed in config or per command.

  24. 24. Why should you avoid cy.wait(5000) in tests?

    Correct answer: Fixed waits are slow and still flaky

    Wait for a condition instead: an element, an assertion or an aliased request with cy.wait('@alias').

  25. 25. What are Cypress commands, in terms of timing?

    Correct answer: Enqueued and run later, one after another

    Calling cy.get() adds it to a queue. Cypress then runs the queue in order, which is why you can't use the return value directly.

  26. 26. Why can't you write const btn = cy.get('button') and use btn as an element?

    Correct answer: Commands return chainers, not elements; the value comes later

    Use .then(($btn) => ...) or chain commands. Aliases with .as() share values between commands.

  27. 27. What does .then() give you in Cypress?

    Correct answer: The yielded subject, to work with in a callback

    .then() doesn't retry. Use .should() with a callback when you need retrying checks.

  28. 28. What does .as('user') do?

    Correct answer: Creates an alias you can reuse with @user

    Aliases work for elements, values and intercepted requests: cy.get('@user') or cy.wait('@getUser').

  29. 29. What does cy.intercept() let you do?

    Correct answer: Observe, wait for or stub network requests

    You can spy on real requests, wait for them, or return fake responses for error and edge cases.

  30. 30. How do you wait for a request before asserting the UI?

    cy.intercept("GET", "/api/orders").as("orders");
    cy.visit("/orders");
    cy.wait("@orders");

    Correct answer: Alias it with cy.intercept and use cy.wait('@orders')

    Defining the intercept before the action ensures it catches the request. cy.wait('@alias') also yields the request and response for assertions.

  31. 31. How do you stub an API response with fixed data?

    Correct answer: cy.intercept('GET', '/api/users', { fixture: 'users.json' })

    A static response or fixture replaces the real server answer, making tests fast and deterministic.

  32. 32. What are fixtures in Cypress?

    Correct answer: Static data files, usually in cypress/fixtures

    Fixtures hold JSON or other data for stubs and test inputs. Load them with cy.fixture() or reference them in cy.intercept.

  33. 33. What does cy.request() do?

    Correct answer: Sends an HTTP request from Node, without the page

    It's great for setting up data or logging in via API, and it isn't subject to browser CORS rules.

  34. 34. Why log in through cy.request or cy.session instead of the UI in most tests?

    Correct answer: UI login in every test is slow and flaky

    Test the login UI once in its own spec. Other tests start already authenticated.

  35. 35. What does cy.session() do?

    Correct answer: Caches and restores cookies and storage for a login

    The setup runs once per session ID; later tests restore the saved state, which speeds up suites a lot.

  36. 36. What is test isolation in Cypress?

    Correct answer: Clearing page, cookies and storage before each test

    With testIsolation: true (the default), each test starts on a blank page with clean state, so tests don't depend on each other.

  37. 37. Why shouldn't one test depend on another test's result?

    Correct answer: Order changes or single runs make it fail

    Each test should set up its own state, so it passes alone, in any order and on retry.

  38. 38. How do you run only one test while working on it?

    Correct answer: Use it.only(...) temporarily

    .only focuses a test or describe. Remove it before committing, or a CI check can fail on it.

  39. 39. How do you skip a test?

    Correct answer: it.skip(...)

    Skipped tests appear as pending in the report, so they stay visible.

  40. 40. Where should code that runs before each test, like cy.visit('/'), go?

    Correct answer: In beforeEach

    beforeEach runs before every test. Code outside hooks and tests runs while specs are collected, not inside a test.

  41. 41. What does cy.viewport('iphone-x') do?

    Correct answer: Sets the window size to that device preset

    It changes the size only. It doesn't emulate the iOS browser engine or touch behaviour fully.

  42. 42. What does cy.screenshot() do?

    Correct answer: Saves a screenshot of the page or element

    Cypress also saves screenshots automatically on failures in cypress run. Visual comparison needs a plugin.

  43. 43. Which browsers can Cypress run tests in?

    Correct answer: Chrome-family browsers, Firefox and WebKit (experimental)

    You pick the browser with --browser. Electron is bundled and used by default in cypress run.

  44. 44. What does the Cypress command log show?

    Correct answer: Each command, with DOM snapshots you can hover over

    Time-travel snapshots show the page before and after each command, which makes debugging much easier.

  45. 45. How do you pause a test to inspect the page in the Cypress app?

    Correct answer: Add cy.pause()

    cy.pause() halts the command queue so you can inspect and then step through. cy.debug() logs the subject and hits a debugger.

  46. 46. How do you scope queries to one part of the page?

    Correct answer: cy.get('form').within(() => { ... })

    Inside .within(), cy.get and cy.contains search only that element, avoiding matches elsewhere.

  47. 47. How do you get the second item from a list?

    Correct answer: cy.get('li').eq(1)

    .eq() is zero-based; .first() and .last() are shortcuts. Filtering by content is often more robust than position.

  48. 48. How do you find a child element of a found element?

    Correct answer: cy.get('.card').find('button')

    .find() searches descendants of the subject. Chaining cy.get would search the whole document again.

  49. 49. How do you read environment-specific values in Cypress?

    Correct answer: Cypress.env('API_URL')

    Env values come from config, cypress.env.json, CYPRESS_ variables or --env. Specs run in the browser, so process.env isn't available there.

  50. 50. Why should secrets not be committed in cypress.env.json?

    Correct answer: Anyone with repo access could read and misuse them

    Inject secrets in CI as CYPRESS_* variables or from a secret store.

  51. 51. What is a custom command?

    Correct answer: A reusable command added with Cypress.Commands.add

    For example cy.login(email, password). Keep them small and focused; heavy logic is easier to maintain as plain functions.

  52. 52. Where are custom commands usually registered?

    Correct answer: In cypress/support/commands.ts

    The support file runs before every spec, so commands defined there are available everywhere.

  53. 53. What does cy.task() do?

    Correct answer: Runs code in the Node process, e.g. to seed a database

    Tasks are registered in setupNodeEvents and can do things the browser can't, like file system or database access.

  54. 54. How do you test a feature that depends on time, like a countdown?

    Correct answer: Use cy.clock() and cy.tick()

    cy.clock() replaces timers and Date, and cy.tick(ms) moves time forward instantly.

  55. 55. What does cy.stub() create?

    Correct answer: A fake function you can control and inspect

    Stubs replace methods, for example window.open, and record calls for assertions.

  56. 56. What happens to a window.alert() in a Cypress test?

    Correct answer: Cypress auto-accepts it; listen via window:alert

    Use cy.on('window:alert', (text) => ...) to assert the message. For confirm, return false from window:confirm to cancel.

  57. 57. Why does Cypress record screenshots on failure in cypress run?

    Correct answer: To show the state of the app when the test failed

    Failure screenshots and optional videos help understand CI failures without rerunning.

  58. 58. What is Cypress component testing?

    Correct answer: Mounting single components in a real browser to test them

    Component tests mount a React, Vue or Angular component with cy.mount() and use the same commands and assertions.

  59. 59. What does the Cypress Testing Library add?

    Correct answer: Queries like cy.findByRole based on accessible roles

    Role and label queries mirror how users find elements and encourage accessible markup.

  60. 60. How can test retries be enabled in Cypress?

    Correct answer: retries: { runMode: 2, openMode: 0 } in config

    Retries help with rare glitches in CI. Tests that pass on retry should still be investigated as flaky.

  61. 61. Why does a hard-coded cy.wait(2000) before checking a result usually fail sometimes?

    Correct answer: The result may take longer than 2 s under load

    Timing varies between machines and runs. Waiting on the condition or request removes the guess.

  62. 62. What should a Cypress test check after submitting a form?

    Correct answer: The visible result and, if needed, the request sent

    Asserting the UI outcome and the intercepted request body catches both display and data bugs.

  63. 63. What does cy.reload() do?

    Correct answer: Reloads the current page

    It's useful to test that state survives a refresh, such as saved preferences.

  64. 64. What does cy.go('back') do?

    Correct answer: Navigates back in browser history

    It helps test history handling and that forms keep or reset data correctly.

  65. 65. How do you test a file upload in Cypress?

    Correct answer: cy.get('input[type=file]').selectFile('photo.png')

    .selectFile() can also simulate drag and drop with { action: 'drag-drop' }.

  66. 66. How do you assert that a text appears somewhere in an element?

    Correct answer: .should('contain', 'Saved')

    contain checks a substring; have.text checks the exact text.

  67. 67. What is the role of the support file cypress/support/e2e.ts?

    Correct answer: Global setup like custom commands and hooks for all specs

    It runs before every spec file, so shared commands and global beforeEach hooks go there.

  68. 68. Why is cy.get('.btn-primary:nth-child(3)') a fragile selector?

    Correct answer: It depends on styling and position, which change often

    A new button or a class rename breaks it. A data-cy attribute or role-based query is more stable.

  69. 69. What does Cypress do with an uncaught exception from the app?

    Correct answer: Fails the test by default

    App errors usually mean bugs. You can handle known ones with Cypress.on('uncaught:exception'), but do it narrowly.

  70. 70. How do you run one spec file from the command line?

    Correct answer: npx cypress run --spec cypress/e2e/login.cy.ts

    Glob patterns work too, e.g. --spec 'cypress/e2e/cart/*'.

  71. 71. What does Cypress Cloud add on top of the open-source runner?

    Correct answer: Recorded runs, parallel runs, flake detection, replay

    It's a paid service. Open-source alternatives exist for parallel runs and reports.

  72. 72. Why should tests clean up or create their own data?

    Correct answer: So they pass in any environment and order

    Seeding via API or cy.task and using unique values keeps tests independent and repeatable.

  73. 73. What is a common mistake when asserting on text that loads asynchronously?

    Correct answer: Reading text with .then() and comparing without retry

    .then() runs once with whatever text exists. .should() keeps retrying until the text arrives.

  74. 74. How does Cypress handle a click that triggers navigation?

    Correct answer: It waits for the new page to load, then continues

    Cypress detects page loads and waits for them, then continues on the new page.

  75. 75. Which kind of tests does Cypress mainly target?

    Correct answer: End-to-end and component tests of web apps

    Cypress focuses on browser-based testing. Load and backend unit tests use other tools.