Table of content

Custom commands in Cypress

In Cypress, a user can create a new command or redefine an existing command then those commands are called custom commands. To decrease the program size these commands are used. It is recommended to add new commands instead of overwriting the existing commands.

  • The custom commands are created in the path cypress/support/command.js
  • Adding a new command named login in the command.js file.
Cypress.Commands.add("login", (username, password) => {
  //adding a new command named login
  cy.get("#user_login").type(username);
  cy.get("#user_password").type(password);
  cy.get("#user_remember_me").click();
  cy.contains("Sign in").click();
});
Adding a new command to the cypress is like adding a new function/method.

Let us call the login command in the program.

describe("custom commands", () => {
  it("should login using the custom commands", () => {
    cy.visit("http://zero.webappsecurity.com/login.html");
    cy.login("username", "password");
  });
});

Output :

custom-commands

Execution of Test programs in Cypress

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions