cypress .each().wrap()。

发布于 2025-02-03 17:38:43 字数 618 浏览 6 评论 0原文

我正在尝试检查表中显示的项目是否有一些断言。我的方式是将所有项目都放在数组中,然后在该数组上迭代。

我的问题:所有断言已经通过,但是柏树跑者仍然需要大量时间来完成CY.Wrap(.invoke(text))作业。

由于这是我的柏树测试的非常核心的命令,具有更有效的功能将很棒。

我的命令:

cy.get('table tbody').within(() => {
        cy.get('tr').each((tr) => {
            cy.wrap(tr.children().eq(index)).invoke('text').then((text) => {
                text = text.trim();
                arrayWithValuesOfTheList.push(text);
            });
        })
            .then(() => {
                //in here all the (quickly passing) assertions are...
            });
    });

感谢提前的任何帮助。感谢大家!

I am trying to check the items shown in my table for some assertions. My way is to put all of the items in an array and then iterate over that array.

My problem: All assertions already passed but the cypress runner still takes a lot of time to finish the cy.wrap(.invoke(text)) jobs.

Since this is a very core command of my cypress tests it would be great to have a more efficient function.

My command:

cy.get('table tbody').within(() => {
        cy.get('tr').each((tr) => {
            cy.wrap(tr.children().eq(index)).invoke('text').then((text) => {
                text = text.trim();
                arrayWithValuesOfTheList.push(text);
            });
        })
            .then(() => {
                //in here all the (quickly passing) assertions are...
            });
    });

Thanks for any help in advance. I appreciate you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2025-02-10 17:38:43

您可以避免包裹该值,会提高速度,但很难说最慢的部分。

const arrayWithValuesOfTheList  = []
cy.get('table tbody tr')
  .each($tr => {
    arrayWithValuesOfTheList.push($tr.children().eq(index).text())
  })
  .then(() => {
    //in here all the (quickly passing) assertions are...
  })
})

You can avoid wrapping the value, will give some increase in speed but it's hard to say what is the slowest part.

const arrayWithValuesOfTheList  = []
cy.get('table tbody tr')
  .each($tr => {
    arrayWithValuesOfTheList.push($tr.children().eq(index).text())
  })
  .then(() => {
    //in here all the (quickly passing) assertions are...
  })
})
稳稳的幸福 2025-02-10 17:38:43

如果您想在单个单元格上断言,请使用.east($ cell => {...})很好,但是如果您需要全柱断言(例如.east()排序,唯一值)变得困难。

要构建适合各种测试的东西,请查看此处的模式。 nofollow noreferrer“>对表进行排序。

这个想法是使用.map()为选定的表行和列创建助手功能。

const { _ } = Cypress

// helpers, reusable
const getColumn = (colIndex) => {
return (rows$) => {
const children$ = _.map(rows$, 'children')
return _.map(children$, `[${colIndex}]`)
}
}

const toStrings = (cells$) => _.map(cells$, 'textContent')

const toNumbers = (texts) => _.map(text, Number)

cy.get('table tbody tr') // rows of table
.then(getColumn(1)) // extract 2nd column
.then(toStrings) // get the text value
.then(toNumbers) // convert if assertions require numeric values

// whole-column assertion example
.should(values => {
const sorted = _.sortBy(values)
expect(values, 'cells are sorted

If you want to assert on individual cells, using .each($cell => {...}) is fine but if you want whole-column assertions (e.g sorted, unique-values) it gets difficult with .each().

To build something adaptable for various tests, take a look at the pattern here Sorting the table.

The idea is to create helper functions using .map() to selected table rows and columns.

const { _ } = Cypress

// helpers, reusable
const getColumn = (colIndex) => {
  return (rows$) => {
    const children$ = _.map(rows$, 'children')
    return _.map(children$, `[${colIndex}]`)
  }
}

const toStrings = (cells$) => _.map(cells$, 'textContent')

const toNumbers = (texts) => _.map(text, Number)


cy.get('table tbody tr')    // rows of table
  .then(getColumn(1))       // extract 2nd column
  .then(toStrings)          // get the text value
  .then(toNumbers)          // convert if assertions require numeric values

  // whole-column assertion example
  .should(values => {
    const sorted = _.sortBy(values)
    expect(values, 'cells are sorted ????').to.deep.equal(sorted)
  })

  // individual value assertion
  .should(values => {
    values.forEach(value => {
      expect(value).to.be.lt(100)
    })
  })

Addressing performance issue

If performance is poor, you can reduce the number of process steps at the Cypress command level by using jQuery-only commands.

This will avoid adding commands to the Cypress queue which is likely to be the slowest part

const arrayWithValuesOfTheList  = []
cy.get('table tbody tr td:nth-child(2)')    // 2nd col of each row
  .then($tds => {                           // only jQuery methods inside
    $tds.text((index, cellText) => {        
      arrayWithValuesOfTheList.push(cellText.trim())
    })
  })
  .then(() => {
    //in here all the (quickly passing) assertions are...
  })
})
蓝海 2025-02-10 17:38:43

您可以做这样的事情。它获取tr一个值一个值,并与正则匹配的模式匹配。

cy.get('table tbody tr').each(($ele) => {
  cy.wrap($ele.text().trim())
    .should('match', /myregexp/)
    .and('not.include', 'some text')
})

You can do something like this. It gets the tr values one by one and matches in against a regex pattern.

cy.get('table tbody tr').each(($ele) => {
  cy.wrap($ele.text().trim())
    .should('match', /myregexp/)
    .and('not.include', 'some text')
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文