I am doing Cypress automation with JavaScript. In this, I want to read value of one JSON file to another JSON file. And based on that want to retrieve other values.
Below is my credentials.json
file:
{
"url": "site.url",
"sausername": "sausername",
"sapassword": "sapassword",
"viewonlyusername": "vouusername",
"viewonlypassword": "voupassword",
}
In the above file, I pass the URL of different environments where I want to run my scripts. ‘sausername’, ‘sapassword’, ‘viewonlyusername’ and ‘viewonlypassword’ are credentials of different users.
Below is the SuperAdminTestData.json
file, which includes required test data for super admin script.
{
"accountName": "account name",
"PartnerAdminAccount": "Automation Partner",
"WholesalePartnerAccount": "Automation Wholesale Partner",
"mitelAccountName": "Platform name",
"siteName": "site name"
}
Now I am using this data in SuperAdminSpec.js
file as below:
describe(“Super Admin – User Roles and Permissions”, function () {
let superAdminTestData, credentials
before(function () {
cy.fixture('credentials').then(function (testdata) {
credentials = testdata
})
//Get test data from 'superAdminTestData' file
cy.fixture('superAdminTestData').then((testData) => {
superAdminTestData = testData
})
})
it("Login with valid credentials", function () {
loginfunction(credentials.url, credentials.sausername, credentials.sapassword) //data from json file
})
it('Navigate to Partner Admin account', function () {
//Click on all customer dropdown
clickAllcustomers()
//Navigate to Partner Admin Account
clickCustomer(superAdminTestData.PartnerAdminAccount) //data from json file
})
})
In this structure, when I want to run my scripts on a stage environment, I have to go and manually change all data in both JSON files (Actually I have more than two JSON files which require changes).
Requirement: I want to add an extra key and value "environment": "stage"
. So when I want to run my data on stage I just have to change value of that key only from test to stage and vice versa. If I pass stage, it will take value for stage.
Question: Can I read value of one JSON file into another? If yes, how?
Please note that I am a beginner in Cypress as well as in automation.
I have not tried anything. But yes go through some solutions from Google and stack overflow as well:
- Key from one Json file to value of another Json file using node/Javascript
- Jq – add a key/value to one JSON file if such key/value exists in another JSON file
but I can’t understand it. Please help me. Thanks!