0% found this document useful (0 votes)
11 views2 pages

Cypress Cheat Sheet

Uploaded by

geronimofg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Cypress Cheat Sheet

Uploaded by

geronimofg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

10. Environment Variables


 Define in `cypress.json`: `{ "env": { "username": "testUser" } }`
 Access: `Cypress.env('username')` - Access environment variables in tests.

11. Screenshots and Videos


 `cy.screenshot('screenshotName')` - Take a screenshot.
 `video: true` - Record videos of test runs (set in `cypress.config.js`).

12. Debugging Timeouts


 `cy.get('selector', { timeout: 10000 })` - Increase timeout for commands.

Other Important Concepts


 **Cypress Command Queue** - Cypress manages commands in a queue to ensure
sequential execution.
 **Automatic Waiting** - Cypress automatically waits for elements and network
requests.
 **Assertions Integration** - Cypress integrates with Chai for intuitive assertions.
 **Test Isolation** - Each test runs in isolation, ensuring consistent results.
 **Retries** - Failed assertions or commands can automatically retry within the timeout.

You might also like