赛普拉斯可以使用嵌套循环吗?

Can nested loops be used in Cypress?

我想知道Cypress是否支持嵌套循环? 在我的例子中,我有一个 table,我需要在其中遍历每一行(行由“data-automationid="DetailsRowFields"" 表示)然后遍历每一行细胞.

虽然我尝试创建一个函数来提取它,但我不确定是否可以完成。

export function getCellData() : Promise<Map<string, string>> {
return new Cypress.Promise((resolve) => {
    const cellMap = new Map<string, string>();
    cy.get('div[data-automationid="DetailsRowFields"]')
        .each((rowElement) => {
            rowElement.children()
                .each((child) => {
                    const ariaIndex = child.attr('aria-colindex');
                    const cellData = child.text();
                    cellMap.set(ariaIndex, cellData);
                });
        })
        .then(() => resolve(cellMap));
});

}

这不会像您预期的那样有效,您只会获得最后一行数据。

映射键应包含行索引。

此外,rowElement 是一个 jQuery 对象。虽然 jQuery 确实有 .children().each() 的方法,但调用模式与等效的 Cypress .each().

不同

我建议包装 rowElement

const cellMap = new Map<string, string>();

cy.get('div[data-automationid="DetailsRowFields"]')
  .each((rowElement, rowIndex) => {

    cy.wrap(rowElement)
      .children()
      .each((child) => {
        const ariaIndex = child.attr('aria-colindex');
        const cellData = child.text();
        const mapKey = `${rowIndex}:${ariaIndex}`
        cellMap.set(mapKey, cellData);
      })
  })