测试用例是:
我失败的代码是:
const decompress = require('decompress');
decompress('C:/Users/Admin/QA/cypress/downloads/test.zip', 'C:/Users/Admin/QA/cypress/downloads');
cy.readFile('C:/Users/Admin/QA/cypress/downloads/firstFile.c').then((fileContent) => {
const expectedContent = `some code to validate`;
expect(fileContent.trim()).to.equal(expectedContent.trim());
遇到错误:
(uncaught exception)TypeError: fs$readFile is not a function
TypeError
The following error originated from your test code, not from Cypress. It was caused by an unhandled promise rejection.
> fs$readFile is not a function
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
node_modules/graceful-fs/graceful-fs.js:118:1
我哪里做错了?发现了相同的测试想法https://github.com/MMCampos1703/unzipCypress但它也不起作用
重新安装了cypress,更新了依赖,用ai分析代码,搜到了同样的错误
当然,这应该可行,但请确保文件路径,
否则我们接下来就是使用一个插件,我们可以获得这些提取的 zip/war 文件。
依赖项:
extract-zip
- https://www.npmjs.com/package/extract-zip
所以在我们的 /cypress.config.js 中
const extract = require("extract-zip");
/**
* Extract zip / war files using extract- zip npm dependency.
* @param {*} source directory where zip/war file is located.
* @param {*} target where to need extract the zipped / war file.
*/
async function extractZip(source, target) {
try {
await extract(source, { dir: target });
} catch (err) {
console.log("Oops: extractZip failed.", err);
}
}
const zippedFiles = [];
const unzipFiles = async function (dirPath) {
const files = fs.readdirSync(dirPath);
await Promise.all(
files.map(async (file) => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
await unzipFiles(dirPath + "/" + file);
} else {
const fullFilePath = path.join(dirPath, "/", file);
const zipFolderName = file.replace(".zip", "");
const warFolderName = file.replace(".war", "");
if (file.endsWith(".zip")) {
zippedFiles.push(zipFolderName);
await extractZip(fullFilePath, path.join(dirPath, "/"));
await unzipFiles(path.join(dirPath, "/", zipFolderName));
}
if (file.endsWith(".war")) {
zippedFiles.push(warFolderName);
await extractZip(fullFilePath, path.join(dirPath, "/", warFolderName));
await unzipFiles(path.join(dirPath, "/", warFolderName));
}
}
})
);
};
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
/**
* This plugin is used to extract zipped files.
* @param {*} dirPath The source, target directory where zipped files
*/
extractZipFiles(zippedFilePath) {
return new Promise((resolve, reject) => {
const OUTPUT_DIR = process.cwd() + zippedFilePath;
const source = process.cwd() + zippedFilePath;
const target = OUTPUT_DIR;
extractZip(source, target);
unzipFiles(target);
resolve(false);
});
},
});
},
},
});
我们如何使用它,在规范文件中,这就是我们如何使用它
cy.task('extractZipFiles, '<zipFileLocation>');