Search code examples
javascriptjsonautomationprotractordata-driven-tests

Protractor data driven - Is displaying 'undefined' when I try to print JSON file


I'm working with protractor and I'm trying to implement data driven.

I'm using this code that I found searching in google.

This is my test.json

{
"username":"testusername"
}

And i'm calling this json from my spec and trying to print in console:

var fs = require('fs');
var content = fs.readFileSync('test/e2e/tests/test.json');
console.log(content.username);

The test is not failing but in the console is print undefined

enter image description here

I'm missing something? or i'm not implementing well the json? Hope you can help me.


Solution

  • You just need to do require(json_file_path). It will work. Try below code.

    var content = require('test/e2e/tests/test.json');
    console.log(content.username);