Cypress Overview
Cypress is a modern end-to-end testing framework designed to test web applications. It focuses on providing a robust and user-friendly experience for writing and running tests directly in the browser.
Following are some key features and benefits:
- Real-Time Reloads
- Fast and Reliable
- Automatic Waiting
- Debugging
- Network Traffic Control
- Built-In Assertions
- Easy Setup
- Cross-Browser Testing
- Integration with CI/CD
- Extensive Documentation and Community Support
Basic Cypress Interview Questions with Answers
If you’re planning to attend a testing role interview then these cypress interview questions with answers can help you to impress the interviewer and bag the role.
Cypress is an end-to-end testing framework for web applications that provides fast, reliable testing for modern web apps.
Install Cypress via npm using npm install cypress --save-dev
.
Open Cypress using the command npx cypress open
from the project directory.
The default structure includes cypress/fixtures
, cypress/integration
, cypress/plugins
, and cypress/support
.
Cypress primarily tests JavaScript, HTML, and CSS. It can interact with any content rendered in the browser.
Use the it
function to define a test case within a describe
block. For example:javascript describe('My First Test', () => { it('Visits the Kitchen Sink', () => { cy.visit('https://example.cypress.io') cy.contains('type').click() cy.url().should('include', '/commands/actions') }) })
Use the cy.visit(url)
command. Example: cy.visit('https://example.com')
.
Use cy.get(selector).should('exist')
. Example: cy.get('.my-element').should('exist')
.
cy.get()
used for? cy.get()
is used to select DOM elements based on a CSS selector.
Use commands like cy.click()
, cy.type()
, and cy.select()
. For example: cy.get('button').click()
.
Cypress handles asynchronous operations automatically through its command queue and chaining.
Fixtures are static data files used to provide mock data for tests. They are stored in the cypress/fixtures
folder.
Load fixtures using cy.fixture('filename').then(data => { /* use data */ })
. Example:javascript cy.fixture('user').then((user) => { cy.get('input[name="username"]').type(user.username) })
cypress.json
? cypress.json
is used to configure Cypress settings such as base URL, viewport size, and environment variables.
Define them in cypress.json
, pass them via the command line, or use Cypress.env('key')
in your tests.
cy.wait()
used for? cy.wait()
pauses the test execution for a specified amount of time or until a specific request finishes.
Use cy.intercept()
to stub or spy on network requests. Example:javascript cy.intercept('GET', '/api/endpoint', { fixture: 'response.json' })
Use cy.clearCookies()
to remove all cookies or cy.clearCookie('cookieName')
for a specific cookie.
cy.reload()
? cy.reload()
refreshes the current page, useful for testing behaviors after a page reload.
Cypress does not support testing multiple tabs or windows directly; instead, it focuses on single-page interactions.
Also Read – Excel Interview Questions for Data Analyst – 100+ Questions with Answers
Intermediate Cypress Interview Questions with Answers
cy.origin()
cy.origin()
allows Cypress to handle cross-origin requests and interactions in tests.
Use cy.get(selector).should('be.visible')
to assert that an element is visible on the page.
Custom commands are user-defined functions added to Cypress’ cy
object to simplify repetitive tasks.
Define it in cypress/support/commands.js
. Example:javascript Cypress.Commands.add('login', (username, password) => { cy.get('input[name="username"]').type(username) cy.get('input[name="password"]').type(password) cy.get('button[type="submit"]').click() })
Use dynamic selectors, cy.wait()
for network responses, or cy.contains()
for text-based assertions.
cy.exec()
command used for? cy.exec()
runs system commands or scripts from within your test. Example: cy.exec('npm run build')
.
Run tests headlessly with npx cypress run
or configure headless mode in cypress.json
.
Use cy.debug()
to pause execution, cy.pause()
to stop execution, or inspect the Cypress Test Runner.
Use cy.screenshot()
to capture a screenshot during test execution.
cy.task()
used for? cy.task()
allows you to execute custom Node.js code from your Cypress tests. Define tasks in cypress/plugins/index.js
.
Configure test retries in cypress.json
using the retries
key. Example: "retries": { "runMode": 2, "openMode": 1 }
.
Cypress plugins extend Cypress functionality. They are defined in cypress/plugins/index.js
.
Adjust timeouts globally in cypress.json
or locally using options in commands. Example:javascript cy.get('button', { timeout: 10000 }).click()
cy.stub()
function used for? cy.stub()
creates a spy or stub for functions or methods, useful for intercepting calls and verifying behavior.
cy.clock()
and cy.tick()
? cy.clock()
allows you to control the system clock, and cy.tick()
advances time, useful for testing time-dependent functionality.
cy.viewport()
? cy.viewport()
sets the size of the browser window for testing responsive designs.
Use the cy.upload()
plugin or interact directly with file input elements. Example:javascript cy.get('input[type="file"]').attachFile('path/to/file.jpg')
Run Cypress in a Docker container to create a consistent test environment. Use the official Cypress Docker image or configure a custom Dockerfile.
Use the Cypress Dashboard Service to parallelize test runs across multiple machines or browsers.
cy.wrap()
function used for? cy.wrap()
converts a value into a Cypress chainable object, allowing Cypress commands to be chained.