Cypress Cheat Sheet
Cypress Cheat Sheet
This cheat sheet provides an overview of the most commonly used Cypress commands,
concepts, and best practices to help streamline your testing process.
1. Basic Commands
`cy.visit('https://example.com')` - Visit a page.
`cy.reload()` - Reload the current page.
`cy.go('back')` - Navigate back in browser history.
2. Element Selectors
`cy.get('selector')` - Select an element.
`cy.contains('text')` - Find an element containing specific text.
`cy.get('parentSelector').children('childSelector')` - Get child elements.
3. Actions
`cy.get('selector').type('text')` - Type into an input field.
`cy.get('selector').click()` - Click on an element.
`cy.get('selector').select('option')` - Select an option from a dropdown.
4. Assertions
`cy.get('selector').should('contain', 'text')` - Assert that an element
contains text.
`cy.get('selector').should('be.visible')` - Assert that an element is visible.
`cy.get('selector').should('have.class', 'className')` - Assert that an
element has a class.
5. Waiting
`cy.wait(1000)` - Wait for a fixed time (e.g., 1 second).
`cy.get('selector', { timeout: 10000 }).should('be.visible')` - Wait for an element.
6. Network Requests
`cy.intercept('GET', '/api/endpoint').as('getData')` - Intercept a network request.
`cy.wait('@getData').its('response.statusCode').should('eq', 200)` - Wait for a response.
7. Hooks
`before(() => { ... })` - Run code once before all tests.
`beforeEach(() => { ... })` - Run code before each test.
`after(() => { ... })` - Run code after all tests.
8. Debugging
`cy.pause()` - Pause execution at a specific command.
`cy.get('selector').debug()` - Print debug information to the console.
9. Custom Commands
Define: `Cypress.Commands.add('login', (username, password) => { ... })`
Use: `cy.login('user', 'password')` - Use custom commands in your tests.