0
votes

I use support/commands.js from cypress to set the functions for the requests to the API or in order to login on the test files.

On my commands.js I have this function.

Cypress.Commands.add("login", () => {
  cy.request({
    method: "POST",
    form: true,
    url: "baseUrl/api/v1/auth/login",
    headers: {
      "Content-Type": "application/json",
    },
    body: {
      email: "testUserEmail",
      password: "testUserPassword",
    },
  })
    .its("body.data")
    .should("exist")
    .then((session_token) => {
      cy.setLocalStorage("default_auth_token", session_token);
      cy.setCookie("rememberMe", "false");
      token = session_token;
    });
});

I want to access the variables on my cypress.json in order to set baseUrl, user and password there and not all spread on my commands.js.

1

1 Answers

2
votes

You can us Cypress.config() to access any values from your cypress.json file. Assuming your cypress.json file looks like this:

{
    "baseUrl": "https://example.com/api/v1/auth/login",
    "username": "admin",
    "password": "password"
}

You can Use these values in your support/commands.js file as:

Cypress.config('baseUrl')
Cypress.config('username')
Cypress.config('password')