Cypress wait-until 插件问题:cy.then() 失败,因为您混淆了异步和同步代码

问题描述 投票:0回答:1

我有这个辅助函数,应该等到状态等于“已发送”。

function waitForSENTStatusDB(GWuuid: string) {
        cy.log("wait for status 'SENT' in DB");
        cy.waitUntil(() => {
            const BodyToUse = "SELECT * FROM table where entity_id = '{GWID}' order by last_modified_at desc".replaceAll("{GWID}", GWuuid);

            DB.Generic(BodyToUse).then((res) => {
                let status = res[0].status;
                cy.log("Current status: " + status);
                return status === INTERFACES.UploadStatus.SENT;
            });
            }, {
                errorMsg: "Timeout: Upload status is not 'SENT'",
                interval: 10000,
                timeout: 200000
            }
            )
        }

我正在 Cypress 的“it”块中调用该函数:

it(`(Iteration: config changed = ${item.PAYLOAD_CHANGE.configurationChanged})Check/Wait for entity state - status SENT (via DB)`, () => {
            waitForSENTStatusDB(GWid);
        });

该函数正在正常等待,一切正常,但是一旦辅助函数返回

true
并继续到下一个
it
块,赛普拉斯在执行过程中会给出以下错误:

cy.then() failed because you are mixing up async and sync code.

In your callback function you invoked 1 or more cy commands but then returned a synchronous value.

Cypress commands are asynchronous and it doesn't make sense to queue cy commands and yet return a synchronous value.

You likely forgot to properly chain the cy commands using another cy.then().

The value you synchronously returned was: true

我是 Cypress 的新手,我尝试了我想到的一切。

当辅助函数 waitUntil() 返回 true 后,执行将继续到下一个 it 块。

cypress cypress-wait-until
1个回答
0
投票

waituntil 命令是有限的,但您可以替换普通的 JavaScript 函数来重复该命令。

由于您已经使用函数进行了包装,因此请向参数添加一个计数器,并在不满足条件时重复调用。

function waitForSENTStatusDB(GWuuid: string, times: 0) {

  cy.log("wait for status 'SENT' in DB", times);

  if (times > 20) {    // counter = timeout/interval
    throw new Error("Timeout: Upload status is not 'SENT'")
  }

  const bodyToUse = `SELECT * FROM table where entity_id = '${GWuuid}' order by last_modified_at desc`

  DB.Generic(bodyToUse).then((res) => {
    let status = res[0].status;
    cy.log("Current status: " + status)

    if (status !== INTERFACES.UploadStatus.SENT) {
      cy.wait(10000)
      waitForSENTStatusDB(GWuuid, ++times)
    }
}


waitForSENTStatusDB('123')
© www.soinside.com 2019 - 2024. All rights reserved.