This question is not the same as: Load local JSON file into variable
I've just tried implementing that change and nothing...
I've been finding that When I run my json string results through my web browser I'm getting
Uncaught ReferenceError: require is not defined
I'm using Node.js as my platform for running this.
After looking into this I'm still no further forward. I don't want to use Browserfy or webpack. And I'm not using Jquery.
This works when I hard code my two json strings into index.js. However I'm need it to be variable.
I need a pure JavaScript way of reading the .json file without using require. As I've seen
let fs = require('fs');
which will enable me to read the file however that's a require...
I have my HTML and Index.js files below if you can suggest anything.
Webpage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test Out Put to HTML Page</title>
</style>
</head>
<body>
<div id ='TestResults'>
<div id = 'TestOutput'>Nothing Yet</div>
</div>
<div id ='ErrorResults'>
<div id = 'ErrorOutPut'>Nothing Yet</div>
</div>
<script type = 'text/javascript' src = 'index.js'></script>
</body>
</html>
Index.js
let myJsonString = require('../../ResultsArray/ResultsArray');
let myJsonStringError = require('../../ResultsArray/ErrorArray');
function result(results){
if(results === 'YesNo')
{
return 'Passed'
}
if(results === 'NoYes')
{
return 'Failed'
}
else
{
return 'Unknown'
}
}
function time (value){
// 1- Convert to seconds:
let seconds = (value / 1000).toFixed(2);
// 2- Extract hours:
let hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
let minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = (seconds % 60).toFixed(2);
return ([hours+":"+minutes+":"+seconds]);
}
function testTemplateErrors (test){
return `
<tr>
<td class='TestGroup'>${test.Folder}</td>
<td class='TestName'>${test.Test_Name}</td>
<td class='TestsStatus'>${test.Running}</td>
<td class='TestRunTime'>${time(test.Run_Time)}</td>
<td class= 'TestResult'>${result(test.Passed + test.Failed)}</td>
</tr>
`
}
function testTemplateResults (test){
return `
<tr>
<td class='TestGroup'>${test.Folder}</td>
<td class='TestName'>${test.Test_Name}</td>
<td class='TestsStatus'>${test.Running}</td>
<td class='TestRunTime'>${time(test.Run_Time)}</td>
<td class= 'TestResult'>${result(test.Passed + test.Failed)}</td>
</tr>
`
}
if(myJsonString && myJsonString.length)
{
document.getElementById('TestResults').innerHTML = `
<h1 class='ContentTitle'> Regression Tests (${myJsonString.length} Tests Ran)</h1>
<table><tr>
<th>Result Folder Name</th>
<th>Result Test Name</th>
<th>Result Running</th>
<th>Result Run Time</th>
<th>Result Result</th>
</tr><tr>
${myJsonString.map(testTemplateResults).join('')}
</tr></table>
<p class='footer'> These ${myJsonString.length} Results were added recently. </p>
`
}
if(myJsonStringError && myJsonStringError.length)
{
document.getElementById('ErrorResults').innerHTML = `
<h1 class='ContentTitle'> Error Results (${myJsonStringError.length} Errors Occurred)</h1>
<table><tr>
<th>Error Folder Name</th>
<th>Error Test Name</th>
<th>Error Running</th>
<th>Error Run Time</th>
<th>Error Result</th>
</tr><tr>
${myJsonStringError.map(testTemplateErrors).join('')}
</tr></table>
<p class='footer'> These ${myJsonStringError.length} Results were added recently. </p>
`
}