You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress使用each()后无法正确返回值的问题求助

问题描述

我写了两段用each()的Cypress代码,用来验证表格里是否存在指定规则名称,遇到了两个问题:

  1. 第一段代码明明应该返回true,却始终返回false。cy.log显示赋值后isFound是true,但return之前变成了false:
verifyRuleInTable(ruleName) {
        const table = cy.get('ex-table[data-testid="deployedapis-table"]').shadow().find('div[role="rowgroup"]');
        const realTable = table.find('div[role="row"]')
        let isFound = false;
        realTable.each(($row, rowIndex) => {
            cy.wrap($row).find('div span span').each(($column,columnIndex) =>{
                if(columnIndex===1){
                    cy.wrap($column).invoke('text').then((result) => {
                        if (result.trim() === ruleName.trim()){
                            isFound = true;
                            cy.log('isFound value after assignment is:' + isFound)
                        }
                    })
                }
            })
        })
        
        cy.log('isFound value before return: ' + isFound)
        return isFound ? true : false;
    }
  1. 修改后的第二段代码,cy.log能正确打印找到/未找到的信息,但把log换成return true/false时,代码返回空值:
verifyPolicyRuleInTable(policyRuleName) {
        const table = cy.get('ex-table[data-testid="deployedapis-table"]').shadow().find('div[role="rowgroup"]');
        const realTable = table.find('div[role="row"]')
        let isFound = false;
        realTable.each(($row, rowIndex) => {
            cy.wrap($row).find('div span span').each(($column,columnIndex) =>{
                if(columnIndex===1){
                    cy.wrap($column).invoke('text').then((result) => {
                        if (result.trim() === policyRuleName.trim()){
                            isFound = true;
                        }
                    })
                }
            })
        }).then(() => {
            if (isFound) {
                cy.log('Value found!');
            } else {
                cy.log('Value not found!');
            }
        });
    }

请问怎么修改代码,让它在isFound为true时返回true,为false时返回false?

问题原因

Cypress的命令都是异步执行的,代码问题出在异步流程的处理上:

  • 第一段代码中,return isFound语句在realTable.each()和invoke('text')这些异步操作完成前就执行了,所以isFound还是初始的false,导致返回结果错误。
  • 第二段代码中,then()块里的逻辑虽然能正确判断isFound,但整个函数没有返回Cypress的命令链,所以外部调用时拿不到返回值。
解决方案

要让函数能正确返回布尔值,需要让函数返回Cypress的命令链,并在最终的then()块里返回isFound的值。修改后的代码如下:

verifyPolicyRuleInTable(policyRuleName) {
  const table = cy.get('ex-table[data-testid="deployedapis-table"]').shadow().find('div[role="rowgroup"]');
  const realTable = table.find('div[role="row"]');
  let isFound = false;

  // 返回整个Cypress命令链
  return realTable.each(($row) => {
    cy.wrap($row).find('div span span').eq(1) // 直接用eq(1)定位第二列,替代嵌套each循环更高效
      .invoke('text')
      .then((result) => {
        if (result.trim() === policyRuleName.trim()) {
          isFound = true;
        }
      });
  }).then(() => {
    // 在then块里返回isFound的值
    cy.log(isFound ? 'Value found!' : 'Value not found!');
    return isFound;
  });
}
额外优化点
  • 用eq(1)直接定位第二列,避免嵌套的each循环,简化代码逻辑。
  • 函数返回命令链后,外部可以通过.then()获取返回值,比如:
verifyPolicyRuleInTable('目标规则名').then((found) => {
  if (found) {
    // 找到后的操作
  } else {
    // 未找到后的操作
  }
});

内容的提问来源于stack exchange,提问作者Jim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:36:17