Giorgio Martini
Giorgio Martini

Reputation: 139

Cypress custom command wont return value

I have a function that I want to add as a command so i can reuse it.

Its on cypress/support/commands.js:

Cypress.Commands.add("generatePassword", () => {

    return 'randomstring';
  }
);

Then on my test I want to use it as:

  it("Visits page", () => {

    const password = generatePassword();
    cy.log({password})
    // Here it logs this:
    //{password: {chainerid: chainer146, firstcall: false}}



  });

Any idea on how to get the actual value? Now i get this:

{chainerid: chainer146, firstcall: false}

Thanks.

Upvotes: 0

Views: 1222

Answers (1)

Srinu Kodi
Srinu Kodi

Reputation: 512

Basically cypress works in promise chain and you're returning the promise chainerid from your custom command. You have to chain it to use in next statement. Use something like below.

it("Visits page", () => {
return cy.generatePassword().then(pwd => {
    cy.log(pwd);
  });
});

Upvotes: 1

Related Questions