Currently, I am facing an issue while trying to extract data from a JSON file using JavaScript. I am able to successfully read data from the file, but when I try to pass this data within my code, it results in errors. Any guidance or assistance on this matter would be greatly appreciated.
Here is the code snippet I am working with:
module.exports = {
'@tags': ['TC2'],
"LAUNCHURL" : function (browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err;
var mydata = JSON.parse(data);
var url_get = mydata.credentials[0]['url']
browser
.url(url_get)
.waitForElementVisible('//body', 1000)
});
},
"Login": function(browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err;
var mydata = JSON.parse(data);
var email = mydata.credentials[0]['email']
browser
.useXpath()
.click("//a[contains(text(),'Sign In')]")
.waitForElementVisible('//body',1000)
.setValue("//input[@aria-label='Enter email address']", email )
.click("//button[@type='button']")
.waitForElementVisible("//input[@id='password']")
.setValue("//input[@id='password']","password12345")
.click("//button[@type='button']")
});
}
};
This is how my JSON file is structured:
{ "credentials": [ {"url": "https://www.walmart.com", "search": "bandaids", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384c5d4b4c784159505757165b5755">[email protected]</a>", "password" : "password12345"}] }
Additional Information: After testing, I noticed that the script only functions correctly when the values are hardcoded. When attempting to dynamically pass in data, the code breaks and fails to execute. I am using Nightwatch as my testing tool.