我想从本地存储库导入JSON数据。
var fs = require('fs');
var data = fs.readFileSync('./profile.json', 'utf8');
console.log(data);
...
然而,Cypress发生此错误:
这是我的cypress项目中的package.json。
{
"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"
}
}
我不想使用cy.fixture,因为我需要在我的“it test discription”中使用json中的数据。例如,我想做这样的测试。
it (datas.data, ()=>{
...
})
3条答案
按热度按时间nc1teljy1#
您应该能够使用require语句加载
profile.json
。olmpazwi2#
要使用fs或其他各种Node模块,您必须将其放入插件中。来自https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Plugins-file:
当Cypress测试在浏览器中执行时,插件文件在后台Node进程中运行,使您的测试能够通过调用
cy.task()
命令访问文件系统和操作系统的其余部分这是在上面的评论中提到的,但我错过了它,所以我想把它弄清楚。
ugmeyewa3#
这是一个相当古老的线程,但现在有可能使用包cypress-fs(我是作者),如下所示:
该软件包在内部使用fs模块,它需要你在
cypress.config.ts
中注册一些支持文件。文档中包含了执行此操作的步骤。