如何在同步循环中运行Cypress命令



我正在开发一个闪卡应用程序,其中每个闪卡都有一个";术语";和一个";定义";。

每个表单字段都可由一个";数据测试";或";术语-{索引值}";或";definition={index-value}";

我试图执行的代码看起来像这样:

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

目前,Cypress不会运行该代码。

我认为我的问题与Cypress的异步性质和上面代码的同步性质有关。有人知道我如何才能让这样的代码正常工作吗?

您可以使用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
})

最新更新