温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - How do you call `request` within a Cypress custom command?
cypress javascript

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

发布于 2020-04-08 00:23:10

您如何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()。
因为赛普拉斯命令已经像承诺一样,所以您不需要包装它们。
或返回您自己的承诺。Cypress将使用来解决您的命令。
无论赛普拉斯最终命令产生什么结果。

查看更多

提问者
GN.
被浏览
66
balzafin 2020-02-01 05:18

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

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

您可以将命令与 then

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