Cypress自定义命令不会返回值

Cypress自定义命令不会返回值,cypress,Cypress,我想添加一个函数作为命令,以便可以重用它 它位于cypress/support/commands.js上: Cypress.Commands.add("generatePassword", () => { return 'randomstring'; } ); 然后在我的测试中,我想将其用作: it("Visits page", () => { const password = generatePassword(); cy.log({passwo

我想添加一个函数作为命令,以便可以重用它

它位于cypress/support/commands.js上:

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

    return 'randomstring';
  }
);
然后在我的测试中,我想将其用作:

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

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



  });
你知道如何得到实际价值吗?现在我明白了:

{chainerid: chainer146, firstcall: false}

谢谢。

基本上cypress在promise chain中工作,您将从自定义命令返回promise chainerid。您必须将其链接起来,以便在下一个语句中使用。使用下面的方法

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

在这种情况下不要使用自定义命令,请使用简单的函数。但我无法导入文件。