【发布时间】:2020-01-11 19:27:29
【问题描述】:
我想输出一个包含很多项目的 JSON。这是我用 7 个参数实现的函数。
let OutputJSON = (testName1,result1,timeStart1,timeEnd1,SEL1,URL1,day) =>{
var resultOBJ;
resultOBJ = {testName:testName1, result:result1, timeStart:timeStart1, timeEnd:timeEnd1, SEL:SEL1, URL:URL1};
var resultjson = JSON.stringify(resultOBJ);
console.log(timeStart1);
var fs = require('fs');
fs.writeFile( `${testName1} ${day} ${timeStart1} Result.json`,resultjson,(err) => {
if (err) throw err;
console.log('Output no problem');
});
}
我在另一个脚本中调用了这个函数。
var Test1 = function(testcase){
var testData = testcase
var folderName = 'broadband'
var fileName = testData.caseName;
var screenShotPath = `screenshots/${folderName}/${fileName}`;
var testResult=false;
var startTime = currentTime();
var endTime;
var day = currentDate();
describe(`Test Case: ${testData.testname}`, function(){
this.timeout(40000);
this.slow(1000);
before(function*(){
prepareScreenshotDir(folderName, fileName);
nightmare = Nightmare(testSetting.nightmare);
d = {
width: 1024,
height: 1200,
}
if (testSetting.authentication) {
yield nightmare.authentication(testSetting.authentication.user, testSetting.authentication.password);
}
yield nightmare
.goto(testData.URL)
.size()
.then((dimension)=>{d= dimension;});
yield takeFullScreenshot(nightmare, d, `${screenShotPath}/${fileName}-${stepCounter}.png` );
});
beforeEach(function*(){
yield nightmare
//.click('#__tealiumModal') //wts that?
.catch(error=>{
console.log(testData.testname + ' Landing page fail.');
testResult=false;
});
});
after(function*() {
// End the Nightmare instance
yield nightmare.end();
endTime = currentTime();
console.log(startTime,endTime,day);
// OutputJSON(testData.testname,testResult,startTime,endTime,1,119,day)
OutputJSON(testData.testname,testResult,startTime,endTime,testData.SEL,testData.URL,day);
});
it('Check page', function*(){
yield nightmare
.wait(testData.SEL) // Coverage search box
.then(() => {
console.log( testData.testname + ' Landing page is loaded.');
testResult = true;
endTime = currentTime();
})
var d = yield nightmare.size();
yield takeFullScreenshot(nightmare, d, `${screenShotPath}/${fileName}-${++stepCounter}.png` );
});
});
}
OutputJSON() 在 after 函数中被调用。问题出在 startTime 参数中。时间开始可以传递给 OutputJSON(),因为开始时间可以传递给 resultOBJ,但是在 fs.writeFile 部分,如果我包含 ${timeStart1} 因为我希望输出 JSON 标题包含时间,则fs 函数将不会被调用。不会输出任何 JSON。如果我删除 ${timeStart1},那么 JSON 可以成功输出。它没有时间变成'PASS_CASE 2019-09-10 Result.json'。
我尝试使用 fs.writeFile(${timeStart1} Result.json 来缩短名称,因为我怀疑这是文件名长度的问题,但实际上不是。JSON文件仍然无法输出。
我也尝试检查输入参数类型是否有问题。但是我现在可以确保传入函数的项目,日期和时间都是字符串。那为什么我可以把天放进标题,但我不能把时间放进去。我不明白。这是我实现的gettime和getdate函数:
let currentTime = function(){
var d = new Date();
var d_string = d.toString();
var d_substring = d_string.substr(16,8);
return d_substring;
}
let currentDate = function(){
var d = new Date().toISOString().slice(0,10);
return d;
}
我希望时间可以放在输出 JSON 标题中。 非常感谢您阅读本文。我很抱歉我的英语不好。
【问题讨论】:
-
对不起大家,我有一些新的更新。现在它记录一个错误:Uncaught Error: ENOENT: no such file or directory, open 'C:\Users\robert.lui\Desktop\test2\PASS_CASE 2019-09-10 12:27:23 Result.json'。跨度>
-
但是当我没有放入timestart1时,即使文件夹中没有与名称匹配的JSON,它也会创建一个新的。为什么我在标题中有timestart1的时候会报错?
标签: javascript node.js json fs writefile