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.
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();
});
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 :