【问题标题】:avoid callback with await cypress doesn't work?使用 await cypress 避免回调不起作用?
【发布时间】:2021-11-18 18:21:32
【问题描述】:

我的夹具存根上有别名,所以这对我有用:

describe("some page", () => {
  beforeEach(() => {
    cy.intercept("/users", {
      fixture: users.json,
    });
    cy.visit("/somewhere");
  });

  it("show something", () => {
    cy.wait("@firstApiCall").then(() => {
      cy.wait("@2ndApiCall").then(() => {
        cy.get("test:something").should("exist");
      });
    });
  });
});

但这行不通?

it("show something", () => {
  await cy.wait("@firstApiCall");
  await cy.wait("@firstApiCall");

  cy.get("test:something").should("exist");
});

另外,如何避免在每个 it 块中重复 cy.wait(apiCall)

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    它可能会失败:

    Unexpected reserved word 'await'.
    

    那是因为await 关键字只能在async 函数中使用,你的回调函数不是异步的。

    你可以写:

    it("show something", async () => {
      await cy.wait("@firstApiCall");
      await cy.wait("@firstApiCall");
    
      cy.get("test:something").should("exist");
    });
    

    但我想知道你为什么要这样做?赛普拉斯很好地按照编写顺序运行命令 (https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Commands-Run-Serially)。

    我也怀疑你的例子是否真的有效:

    describe("some page", () => {
      beforeEach(() => {
        cy.intercept("/users", {
          fixture: users.json,
        });
        cy.visit("/somewhere");
      });
    
      it("show something", () => {
        cy.wait("@firstApiCall").then(() => {
          cy.wait("@2ndApiCall").then(() => {
            cy.get("test:something").should("exist");
          });
        });
      });
    });
    

    您没有别名 firstApiCall2ndApiCall,所以赛普拉斯不知道该等待什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 2011-10-23
      • 1970-01-01
      • 2011-01-24
      • 2018-10-31
      • 2021-07-06
      • 2021-12-01
      • 2017-07-16
      相关资源
      最近更新 更多