07 Custom Commands - biswajitsundara/Cypress GitHub Wiki

  • We can create new custom commands or modify existing cypress commands
  • These custom commands are nothing but a bunch of reusable code wrapped into a function
  • The place to define or overwrite commands is in cypress/support/commands.js file
  • Since this file is loaded before any test files are evaluated
  • Remember never jump on to create so many custom commands it will over complicate things.
  • Select a suitable code block to put it as custom command
  • For example, login & logout can be created as custom commands

commands.js

Cypress.Commands.add("login", (email, password) => {
    cy.get('#email').type(email);
    cy.get('#passwd').type(password);
    cy.get('#SubmitLogin').click();
})

CustomCommandTest.js

/// <reference types="cypress" />

describe('E commerce Test Suite', () => {

    it('Basic practice website Test', () => {

        cy.visit("http://automationpractice.com/index.php?controller=authentication&back=my-account");
        cy.login('[email protected]','automation')

        cy.get('.account').invoke('text').then((text => {
            expect(text.trim()).to.eq('abc');

        }));
    })

})

⚠️ **GitHub.com Fallback** ⚠️