Cypress – Iterate over each element that takes to new page- perform some action on new page- Do this for all elements

huangapple go评论49阅读模式
英文:

Cypress - Iterate over each element that takes to new page- perform some action on new page- Do this for all elements

问题

[![在此输入图像描述](https://i.stack.imgur.com/ZdGlH.png)](https://i.stack.imgur.com/ZdGlH.png)

[![在此输入图像描述](https://i.stack.imgur.com/U58Ic.png)](https://i.stack.imgur.com/U58Ic.png)

我在 Cypress 中遇到以下问题:
1. 获取所有删除按钮。
2. 遍历每个删除按钮。
3. 单击每个按钮。
4. 转到删除确认页面(它是完全不同的页面,而不是弹出窗口)
5. 单击(红色的)删除按钮(将返回到列表页面)
6. 对其余的购物清单执行相同的操作

```javascript
cy.get('a[href*=delete][href$=confirm]').each(($ele) => {
  cy.wrap($ele).click({ force: true });
  // 在确认删除屏幕上
  cy.contains('button', 'Delete').click({ force: true });
});
英文:

Cypress – Iterate over each element that takes to new page- perform some action on new page- Do this for all elements

Cypress – Iterate over each element that takes to new page- perform some action on new page- Do this for all elements

I have below issue in cypress:

  1. Get all delete buttons.

  2. Iterate over each delete button.

  3. click on each button.

  4. Navigate to delete confirmation page(it is different page all together not a popup)

  5. Click on (red) Delete button (will be navigated back to list page)

  6. Perform same action on remaining shopping lists

    cy.get('a[href*=delete][href$=confirm]').each(($ele) => {
    cy.wrap($ele).click({ force: true });
    // on confirm delete screen
    cy.contains('button', 'Delete').click({force: true,});
    

答案1

得分: 1

尝试在循环内重复获取

const selector = 'a[href*=delete][href$=confirm]';
cy.get(selector).each(($el, index) => {

  cy.get(selector).first()
    .click({ force: true })

  // 在确认删除屏幕上
  cy.contains('button', 'Delete').click({force: true,})

  cy.contains('h1', 'Name').should('be.visible')  // 确认我们回到了第一页

})
英文:

Try repeating the get inside the loop

const selector = 'a[href*=delete][href$=confirm]'
cy.get(selector).each(($el, index) => {

  cy.get(selector).first()
    .click({ force: true })

  // on confirm delete screen
  cy.contains('button', 'Delete').click({force: true,})

  cy.contains('h1', 'Name').should('be.visible')  // confirm we are back on 1st page

})

</details>



huangapple
  • 本文由 发表于 2023年2月16日 17:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定