【发布时间】:2019-11-06 11:47:18
【问题描述】:
我正在尝试使用 for 子句循环测试,因为我只想从 JSON 外部文件(具有许多节点和子节点)中获取数据。 我收到“没有要运行的测试”错误。 我使用 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"
}]
}
【问题讨论】:
-
你的
for-loop不应该是这样的吗?for(var i = 0; i < data.jsonNode.length; i++)而不是这个:for(var i = 0; i < data.jsonNode[i].length; i++)?请注意,中断条件不是指jsonNode[i].length,而是指jsonNode.length -
否,因为我想遍历 JSON 文件中的 jsonNode 节点,而不是整个文件。所以长度指的是 JSON 节点。
-
如果你从
data.json发布几个nodes会更容易 -
你的意思是
for(var i = 0; i < data[ "jsonNode" + i].length; i++)? -
@PeterPaulKiefer 甚至
for(var i = 0; i < data[ "jsonNode" + (i === 0 ? '' : i)].length; i++)据我所知
标签: javascript node.js testing automated-tests testcafe