Signup
Learn about custom signup commands.
We'll cover the following
Introduction
At least half of RealWorld features require a user to be authenticated. By default, Cypress does not allow you to share the state between the tests (which would otherwise be an anti-pattern). We need to transform the signup process into something usable by every test: a Cypress custom command.
A Cypress Custom Command is a function that can be called from the global cy
object. We are going to create a cy.signupV1()
command to be run before every other test.
Steps to create a new Cypress custom command:
-
Create a new file containing the code of the command itself in the
cypress/support
directory. The file should be:cypress/support/signup/signup-v1.js
. -
Add the wrapper of the code.
Cypress.Commands.add("signupV1", (/* the params of the command */) => {
/* the code of the command */
});
- Import the newly created file from the
cypress/support/commands.js
file.
import "./signup/signup-v1";
- Start writing a new test that calls the
cy.signupV1()
command.
context("Whatever test", () => {
it("Should leverage the custom registration command", () => {
cy.signupV1();
});
});
- Run the test!
While the new command does nothing at the moment, it will be the foundation of cy.signupV1
command.
First signup command
The core of the command is the same as the following code.
Get hands-on with 1400+ tech skills courses.