gpt4 book ai didi

javascript - 是否可以在 TestCafe 中循环测试?

转载 作者:太空宇宙 更新时间:2023-11-03 22:04:18 25 4
gpt4 key购买 nike

我尝试使用 for 子句循环测试,因为我想简单地从 JSON 外部文件(有许多 Node 和子 Node )获取数据。我收到“没有要运行的测试”错误。我使用 TestCafe 1.6.0 和 TestCafe Studio 1.1.0。

这里是一些示例代码:

import { t } from 'testcafe';
import {Selector} from 'testcafe';
import {Role} from 'testcafe';
import {helperFunc1, helperFunc2} from '../helper.js';
const fs = require('fs');
const path = require("path");
const fetch = require("node-fetch");
const rawdata = fs.readFileSync(path.resolve(__dirname, "../data.json"));
var data = JSON.parse(rawdata);

fixture `Test`
.page `http://www.testpage.com`
.beforeEach(t => t.resizeWindow(1920, 1080))

for(var i = 0; i < data.jsonNode[i].length; i++)
{
test(`Test - 1`, async t => {await helperFunc1(data.jsonNode[i]);

test(`Test - 2`, async t => {await helperFunc2(data.jsonNode[i], "All", "#HASH"); });
}

data.JSON 示例

{
"jsonNode": [
{

"test1": "A",
"test2": "101",
"test3": "2",
"test4": "4"
},
{

"test1": "B",
"test2": "102",
"test3": "3",
"test4": "5"
}],

"jsonNode1": [
{

"test10": "A",
"test11": "101",
"test12": "2",
"test13": "4"
},
{

"test10": "B",
"test11": "102",
"test12": "3",
"test13": "5"
}]
}

最佳答案

使用 TestCafé 绝对可以进行循环测试,其结构与您描述的结构非常接近。

给定以下 JSON 文件,存储在“dataset.json”中:

{
"nodes": [
{
"id": 0,
"name": "MyFirstNode",
"foo": "bar1"
},
{
"id": 1,
"name": "MySecondNode",
"foo": "bar2"
},
{
"id": 2,
"name": "MyThirdNode",
"foo": "bar3"
},
]
}

TestCafé 可以通过以下实现在“Node ”上循环:

// JSON dataSet containing array on which we will loop the tests 
const dataSet = require( __dirname + 'dataset.json' );

fixture('MyLoopingTests')
.page('http://www.example.com');

// Getting the 'nodes' as a const for readability purpose
const nodes = dataSet.nodes;

nodes.forEach((nodes) => {
test(`Run the tests for "${node.name}"`, async t => {

// My test details for 'node'
});
})

产生以下输出:

MyLoopingTests
✓ Run the tests for "MyFirstNode"
✓ Run the tests for "MySecondNode"
✓ Run the tests for "MyThirdNode"

关于javascript - 是否可以在 TestCafe 中循环测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58729461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com