您如何在赛普拉斯自定义命令中调用“ request”?

时间:2020-01-31 20:49:17

标签: javascript cypress

如何在赛普拉斯自定义命令中调用request

Cypress.Commands.add('factory', async (name, attributes) => {
  const response = await cy.request('POST', '/cypress/factories', {
    name,
    attributes: attributes || {}
  });
  return response;
});

结果为..

返回承诺的命令是:> cy.factory()。
您在promise中调用的cy命令是:> cy.request()。
因为赛普拉斯命令已经像promise一样,所以您不需要将它们包装起来。
或返回您自己的承诺。Cypress将使用。
解决您的命令。 不管最终的Cypress命令产生什么。

1 个答案:

答案 0 :(得分:0)

就像消息中说的那样,赛普拉斯将为您解决响应,因此无需返回任何内容。

Cypress.Commands.add('factory',(name, attributes) => {
  cy.request('POST', '/cypress/factories', {
    name,
    attributes: attributes || {}
  });
});

您可以将命令与then

一起使用
cy.factory(name, attributes).then(response => {
  console.log(response)
})