【发布时间】:2020-09-08 16:07:08
【问题描述】:
我想从我的 excel 表的特定单元格中读取数据,然后在我的 cypress 测试中使用该数据。文件名为“qaautomation.xlsx”,工作表名称为“Input”,我想从单元格 B2 中读取数据。我编写了以下代码来访问该值
/// <reference types ="cypress" />
var xlsx = require ("xlsx");
var workbook= xlsx.readFile("qaautomation.xlsx");
var worksheet= workbook.Sheets["Input"];
var cellB2value= 'B2';
var cellB2=worksheet[cellB2value];
var cellB2_value=(cellB2.v);
下面编写的代码是使用该值的地方。
describe('Typing the address', function(){
it ('should open the link', function(){
cy.visit(cellB2_value) //here comes the value from cell B2
})})
当我运行上面的代码时,我得到以下错误
The following error originated from your test code, not from Cypress.
> _fs.readFileSync is not a function
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
Check your console for the stack trace or click this message to see where it originated from.
有什么办法可以消除这个错误吗?
【问题讨论】:
-
这能回答你的问题吗? JavaScript error: "is not a function"
-
您需要创建一个可以从测试中调用的任务,将您的 xlsx 处理代码放入
cypress/plugins/index.js。请参阅 blog 获取教程。 -
@eric99 阅读上述博客并点击此链接docs.cypress.io/api/commands/task.html#Event,我可以读取我的 excel 文件并直接在我的 cypress 测试中使用。感谢您的指导
标签: javascript node.js cypress xlsx readfile