【问题标题】:How can I run a Cypress Command within a synchronous loop? [duplicate]如何在同步循环中运行赛普拉斯命令? [复制]
【发布时间】:2021-03-18 05:49:45
【问题描述】:

我正在开发一个抽认卡应用程序,其中每个抽认卡都有一个“术语”和一个“定义”。

每个表单字段都可以通过“term-{index value}”或“definition={index value}”的“data-testid”访问

我一直在尝试执行的代码如下所示:

let i = 0
let randomNumber = Math.floor(Math.random() * 20))
while (i < randomNumber) {
  // Run Cypress code using "i" variable value
}

目前,赛普拉斯不会运行代码。

我认为我的问题与 Cypress 的异步特性和上述代码的同步特性有关。有人会碰巧知道我怎样才能让这样的代码工作吗?

【问题讨论】:

  • 这很完美!非常感谢!

标签: javascript loops cypress e2e-testing


【解决方案1】:

您可以使用each() 遍历类似数组的结构(具有长度属性的数组或对象)。比如:

cy.get('data-testid').each(($el, index, $list) => {
    //$el is the unique element
    //index starts with 0 and with each iteration its value increases by 1
    //$list contains the number of unique elements
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 2022-11-17
    • 2022-07-21
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多