Testcafe:foreach计数器

问题描述

我正在使用foreach从js文件读取数据。有没有办法找出当前循环中的索引?

示例:我有10行数据。我想每次运行测试块时都打印索引(1,2..10)。在一个简单的for循环中,我将使用计数器变量(例如i)。

X-Forwarded-Host

解决方法

let i = 0;
urlMain.forEach(data => {
    console.log(++i);
    test("Lens",async t => {
        await t.navigateTo(data.new);
        await t.takeScreenshot()
    })
})
,

扩展Alex的答案,我们可以在Testcafe 1.8.4中使用2个计数器来使其工作。

let i = 0;
let j = 0;
const testName = "Lens"
urlMain.forEach(data => {
    i = i + 1;
    test(testName + "_" + i,async t => {
        j = j + 1;
        const sPath = testName + "_" + j + "/actual.png";
        await t.navigateTo(data.new);
        await t.maximizeWindow()
        await t.wait(5000)
        console.log(sPath);
        await t.takeScreenshot({
            path: sPath,fullPage: true
        });
    })
})