loading...

Cypress Automation Tool Overview

Installation

npm install cypress --save-dev

Opening Cypress

npx cypress open

Running Tests Headlessly

npx cypress run

Running Specific Test File

npx cypress run --spec "cypress/integration/example_spec.js"

Describe Block

describe('Test Suite', () => {
  // Contains multiple it blocks
});

It Block

it('Test Case', () => {
  // Test steps for a single test case
});

Before and After Hooks

before(() => {
  // Runs before all tests in the block
});
after(() => {
  // Runs after all tests in the block
});

BeforeEach and AfterEach Hooks

beforeEach(() => {
  // Runs before each test in the block
});
afterEach(() => {
  // Runs after each test in the block
});

Clicking an Element

cy.get('button').click();

Typing into an Element

cy.get('input').type('Hello, World!');

Asserting Text Content

cy.get('h1').should('have.text', 'Welcome');

Asserting Element Visibility

cy.get('.alert').should('be.visible');

Stubbing a GET Request

cy.intercept('GET', '/users', { fixture: 'users.json' });

Waiting for a Request to Complete

cy.wait('@getRequestAlias');

Testing a POST Request Body

cy.intercept('POST', '/submit', (req) => {
  expect(req.body).to.include({ key: 'value' });
});

Asserting on Request Response

cy.request('https://api.example.com/data')
  .its('body')
  .should('include', { key: 'value' });

Setting Base URL

{
  "baseUrl": "http://localhost:8000"
}

Customizing Timeout

{
  "defaultCommandTimeout": 10000
}

Running on Different Browser

npx cypress run --browser chrome

Recording Test Runs

npx cypress run --record --key <record-key>

Debugging a Test

// Add .debug() to any command to debug it in the console
cy.get('.selector').debug();

Pause Test Execution

cy.pause();

Logging to the Console

cy.log('This will appear in the console');

Including Screenshots and Videos

{
  "screenshotOnRunFailure": true,
  "video": true
}
login
signup