I want to import JSON data from my local repository.
var fs = require('fs');
var data = fs.readFileSync('./profile.json', 'utf8');
console.log(data);
...
However Cypress occurs this error:
Here is my package.json from my cypress project.
{
"name": "cypress_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "npx cypress run",
"clean-reports": "rm -rf cypress/reports",
"merge-report": "npx mochawesome-merge --reportDir cypress/reports/separate-reports cypress/reports/full_report.json",
"generate-report": "npx mochawesome-report-generator --reportDir cypress/reports cypress/reports/full_report.json",
"after:tests": "npm run merge-report; npm run generate-report",
"cypress": "npm run clean-reports; npm run test; npm run after:tests"
},
"keywords": [],
"type": "module",
"author": "",
"license": "ISC",
"dependencies": {
"@cypress/webpack-preprocessor": "^5.4.1",
"cypress": "^4.11.0",
"fs": "0.0.1-security",
"mocha": "5.2.0",
"mochawesome": "4.1.0",
"mochawesome-merge": "2.0.1",
"mochawesome-report-generator": "4.0.1",
"papaparse": "^5.2.0",
"selenium-webdriver": "^4.0.0-alpha.7",
"xlsx": "^0.16.4"
},
"devDependencies": {
"@babel/core": "^7.11.0",
"@babel/preset-env": "^7.11.0",
"babel-loader": "^8.1.0",
"webpack": "^4.44.1"
}
}
I don't want to use cy.fixture because I need to use data from json in my 'it test discription'. For example, I want to make tests like this.
it (datas.data, ()=>{
...
})
You should be able to load your profile.json
with the require statement.
const data = require('profile.json');
To use fs, or various other Node modules, you must put it in a plugin. From https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Plugins-file:
While the Cypress tests execute in the browser, the plugins file runs in the background Node process, giving your tests the ability to access the file system and the rest of the operating system by calling the
cy.task()
command
This is alluded to in the comments above, but I missed it there so I want to make it clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With