Cypress任务失败,并抱怨任务事件未在插件文件中注册
我正在使用cypress
cy.task()方法/函数将csv文件从一个目录复制到另一个目录。下面是我的cy.task('copycsvFile')和support/index.js文件中写的相关代码。运行时抛出以下错误;
CypressError:cy.task(‘copy csvFile’)失败,出现以下错误:
The 'task' event has not been registered in the plugins file. You must register it before using cy.task()是否知道无法识别的原因?
节点版本:v10.15.3,
Cypress版本:3.1.5
//sample-spec.js文件
cy.task('copycsvFile');
下面是我的index.js文件
//support/index.js文件
const fs = require('fs');
module.exports = (on) => {
on('task', {
copycsvFile: (Obj)=>{
var fs = require('fs');
fs.createReadStream('C:/Users/SomeName/Downloads/Export_Survey_CSV.csv').pipe(fs.createWriteStream('C:/User/Client/Client - Cypress Web UI Tests/cypress/fixtures/Export_Survey_CSV.csv'));
}
});
};
解决方案
我终于找到答案了。
我在错误的位置添加了下面的代码,这就是它失败并返回上述错误的原因。
现在我已经更正了位置,并在plugins/index.js下添加了内容,并且工作正常。
我还做了一个小改动,即我增加了return null,因为我的箱子里没有任何东西可以退货。
//在我的规范文件中;
cy.task('copycsvFile');
//在..下添加了以下代码/plugins/index.js
const fs = require('fs');
module.exports = (on) => {
on('task', {
copycsvFile: (Obj)=>{
var fs = require('fs');
fs.createReadStream('C:/Users/SomeName/Downloads/Export_Survey_CSV.csv').pipe(fs.createWriteStream('C:/User/Client/Client - Cypress Web UI Tests/cypress/fixtures/Export_Survey_CSV.csv'));
return null;
}
});
};
相关文章