Closed nurzhan-kukeyev closed 5 months ago
In addition to the question: We want to get all .json files from project directory via JavaScript:
Example:
In env variable where .json files stored: myPath = "D:\Folder"
JavaScript:
myPath= tc.getVar("myPath");
const fs = require('fs');
const path = require('path');
fs.readdir(myPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
});
JavaScript gives an error:
Script Log: Error reading directory: {
"errno": -4058,
"code": "ENOENT",
"syscall": "scandir",
"path": "C:\\Users\\<user>\\AppData\\Local\\Programs\\Microsoft VS Code\\Folder"
}
JS uses the project folder of VS Code
C:\Users\
\AppData\Local\Programs\Microsoft VS Code
Question: How to pass from TC to JavaScript the correct path of the folder? Are there any ways?
You cannot read multiple files in the same test case in the UI.
from script it's possible, try using promises
const fs = require('fs').promises;
// Function to read directory
try {
// Directory path
const directoryPath = tc.getVar('myPath')';
// Read directory asynchronously with await
const files = await fs.readdir(directoryPath);
// List file names
console.log('Files in the directory:');
files.forEach(file => {
console.log(file);
});
} catch (err) {
console.log('Error reading directory:', err);
}
Hi Ranga, thanks for the fast reply. Unfortunately, this solution with premises didn't work for us:
JS takes the path where VSCode installed (C:\User...\Appdata)
Maybe other solutions?
Looks like the path is wrong Ref - https://stackoverflow.com/a/43260706/4035948
can you open the path manually in file explorer and test it
I am closing this issue, feel free to continue discussion
Question: The question is: how can we read (readFile) more than one file in parameters and set contents of files to env.variables ?
Are you using the free version/paid version/trial: paid user - collegue from erwin (deutsche bahn)