To read the contents of a JSON file in Playwright, you'll need to utilize Node.js and the built-in fs module.
Here is the JSON file.
{
"username":"luffy@marine.com",
"password":"GomuGomu"
}
const filePath = 'D:/iVagus/Playwright/user.json';
const fileContents = fs.readFileSync(filePath, 'utf-8');
const jsonData = JSON.parse(fileContents);
console.log(jsonData);
The code above uses fs.readFileSync to read the file synchronously, assumes the file is encoded in UTF-8, and parses the JSON contents with JSON.parse.
You can conduct any additional processes after reading the JSON data.
const userData = JSON.parse(JSON.stringify(require("D:/iVagus/Playwright/user.json")))
We can also import the JSON file like the above and use it to extract information from the variable.
console.log(userData.username);
console.log(userData.password);
