我收到此 Excel 文件读取错误:
赛普拉斯错误
cy.task('readXlsx') 失败并出现以下错误:
xlsx.read 不是函数
我使用带有 typescript 的 cypress 12.7
我在 cypress.config.ts 中写道:
on("task", {
readXlsx({filePath, sheetname}) {
const buf = fs.readFileSync(filePath);
const workbook = xlsx.read(buf, { type: 'buffer' });
const rows = xlsx.utils.sheet_to_json(workbook.Sheets[sheetname]);
return rows
},
});
在 cy-commands.ts 中我定义了命令:
Cypress.Commands.add('readXlsx', (InputFile, string) => cy.task(
'readXlsx',
{ filePath: InputFile, sheetName: string },
));
进一步使用index.d.ts中的命令:
readXlsx(filePath: InputFile, sheetName: string): Chainable<CyQElem>;
函数/任务 readXlsx 我这样调用:
cy.get<string>('@LASTDOWNLOADEDFILE').then((templateFile) => {
// create the upload file name.
cy.log(`TEMPLATE FILE NAME: ${templateFile}`); // Correct
cy.readXlsx(`${templateFile}`, 'SheetName1').then((rows) => {
// first row = header row: now we need the cells...
cy.log(`ROW: ${rdw}`);
expect(rows[0].data[1]).to.contain(data);
cy.log(`ROW DATA: ${rows[0].data[1]}`);
});
但无论我做什么总是失败:
下面是以下异常,它没有提供任何附加信息:
From Node.js Internals:
TypeError: xlsx.read is not a function
at readXlsx (/local0/xxxxxxx/cypress.config.ts:113:33)
at invoke (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_plugins.js:235:16)
at <unknown> (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/util.js:59:14)
at tryCatcher (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/bluebird/js/release/util.js:16:23)
at Function.Promise.attempt.Promise.try (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/bluebird/js/release/method.js:39:29)
at Object.wrapChildPromise (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/util.js:58:23)
at RunPlugins.taskExecute (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_plugins.js:241:10)
at RunPlugins.execute (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_plugins.js:161:21)
at EventEmitter.<anonymous> (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_plugins.js:258:12)
at EventEmitter.emit (node:events:527:28)
at EventEmitter.emit (node:domain:475:12)
at process.<anonymous> (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/util.js:33:22)
at process.emit (node:events:527:28)
at process.emit (node:domain:475:12)
at process.emit.sharedData.processEmitHook.installedValue [as emit] (/local0/jonas_home/.cache/Cypress/12.7.0/Cypress/resources/app/node_modules/@cspotcode/source-map-support/source-map-support.js:745:40)
at emit (node:internal/child_process:938:14)
at processTicksAndRejections (node:internal/process/task_queues:84:21)
这不是错误的原因,但它回答了您的一个问题:
自定义命令和它的类型定义之间似乎混淆了。
Cypress.Commands.add('readXlsx', (InputFile, string) => cy.task(
'readXlsx',
{ filePath: InputFile, sheetName: string },
))
你已经将任务参数复制到类型定义中了,不过应该是这样的
readXlsx(InputFile: string, string: string): Chainable<CyQElem>;
改进命名将对您有很大帮助,例如调用任务
readXlsxTask
以区别于自定义命令名称。
加上
string
作为参数名称与类型 string
混淆,也许是这样:
Cypress.Commands.add('readXlsx', (filePath, sheetName) => {
cy.task('readXlsxTask', { filePath, sheetName })
})
// index.d.ts
readXlsx(filePath: string, sheetName: string): Chainable<CyQElem>;