为什么 cypress 在 for 循环中只循环一次

khi*_*hiz 1 automation typescript cypress cypress-each

为什么 cypress 在 for 循环中只循环一次?

测试代码是这样的:

cy.get('body').contains('Automation').each(($el, index) => {
  cy.get('body').contains('Automation').parents()
    .eq(1)
    .find('mfc-dropdown > div > mfc-button > button', { timeout: 6000 })
    .first()
    .click({ force: true });
  cy.get(this.DELETE_FILE_BUTTON).click();
  cy.get('.mfc-dialog-container')
    .find(this.CONFIRM_DELETE)
    .click({ force: true });
});
Run Code Online (Sandbox Code Playgroud)

Mic*_*nes 5

@JessefSpecialisterren 给出了不发生循环的正确原因。

你能为这个做什么?

您可以使用:contains() 伪选择器contains移动内部get()

描述:选择包含指定文本的所有元素。

cy.get(':contains(Automation)').each(($el, index) => {
Run Code Online (Sandbox Code Playgroud)

您确实想直接定位元素,而不是<body>元素

例如

cy.get('button:contains(Automation)').each(($el, index) => {
Run Code Online (Sandbox Code Playgroud)

第2-3行呢?

这看起来很可疑,我想你想要

cy.get('button:contains(Automation)').each(($el, index) => {
  cy.wrap($el).parents().eq(1)
  ...
Run Code Online (Sandbox Code Playgroud)