For Protractor/JavaScript , use JSON for data driven testing.
JSON example:
{
[
{"username":"User1", "password":"Password1"},
{"username":"User2", "password":"Password2"},
{"username":"User3", "password":"Password3"}
]
}
You may store data in a json file and simply read into a variable and use it as required:
var testData = require('../example/Test Data/Test.json');
describe('LoginPage', function() {
it("data.description", function () {
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(testData[0].username);
element(by.model("password")).sendKeys(testData[0].password);
element(by.buttonText("Authenticate")).click();
});
});
});
By using JSON , you may define complex data hierarchy upto Nth level deeply nested if required.
Complex data definition example using Json:
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
If we loaded this object into a JavaScript program, parsed in a variable called superHeroes for example, we could then access the data inside it using the dot/bracket notation as below:
superHeroes.homeTown
superHeroes['active']
To access data further down the hierarchy, you simply have to chain the required property names and array indexes together. For example, to access the third superpower of the second hero listed in the member's list, you'd do this:
superHeroes['members'][1]['powers'][2]
Alternatively for more flexible & complex data definitions, one may store data as JS objects in which is a bit more flexible compared to JSON files.
JS Object Example:
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
Source:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON
https://www.w3schools.com/js/js_json.asp