0
votes

I'm trying to set up a custom command to add some session storage items in a project and it doesn't seem to be firing.

The command is as follows

Cypress.Commands.add("login", () => {
    window.sessionStorage.setItem("token", "tokengoeshere");
    window.sessionStorage.setItem("username", "phoenix");
    cy.visit("http://localhost:8080");
});

I have added the file into cypress.json

"supportFile": "tests/e2e/support/index.js",

and the index.js looks like

// Import commands.js using ES2015 syntax:
import "./commands";

When the tests fire session storage is empty

1

1 Answers

1
votes

Where are you calling that custom command?

You usually create custom commands in cypress/support/commands.js file and that makes those commands available under cy.

So paste this to your cypress/support/commands.js

Cypress.Commands.add("login", () => {
    window.sessionStorage.setItem("token", "tokengoeshere");
    window.sessionStorage.setItem("username", "phoenix");
    cy.visit("http://localhost:8080");
});

and then call it with cy.login() from any test file. Those are usually in cypress/integration folder. For example in your case a file cypress/integration/myTestsWithCustomCommand.js:

describe("My tests using custom commands", () => {

  it("1st test using custom command", () => {
    cy.login();
    // rest of your code test
  });
});