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, ()=>{
...
})
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, ()=>{
...
})
Share
Improve this question
edited Aug 1, 2020 at 12:54
halfer
20.4k19 gold badges108 silver badges201 bronze badges
asked Aug 1, 2020 at 12:21
loone96loone96
8493 gold badges16 silver badges27 bronze badges
5
|
3 Answers
Reset to default 7You 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.
This is quite an old thread, but there is now the possibility to use the package cypress-fs (which I am the author of) like so:
cy.fsReadFile(path, options)
The package is internally using the fs module, it requires you to register some support file in your cypress.config.ts
. Steps to do so are included in the documentation.
fs.readFileSync
within a Cypress test you must put them in a task, see first example in the docs. But why do you not usecy.fixture()
? – user12697177 Commented Aug 2, 2020 at 9:39cy.fixture()
in abefore()
at the top of the spec. – user12697177 Commented Aug 4, 2020 at 21:28cy.fixture()
will parse the JSON to an object butcy.readFile()
will give you the file contents as a string (same asfs.readFileSync()
if used in a task). – user12697177 Commented Aug 4, 2020 at 21:38