gpt4 book ai didi

cypress - 在 Cypress 中读取 excel 文件

转载 作者:行者123 更新时间:2023-12-04 09:55:13 31 4
gpt4 key购买 nike

我是 Cypress 的新手。如何使用 Cypress 从 excel 文件中读取数据?在谷歌中搜索但找不到有用的答案。

最佳答案

在柏树中你可以创建cypress task使用 SheetJS 读取 xlsx 文件图书馆。

用法

cypress\integration\read-xlsx.spec.js

context('Xlsx file', () => {
it('Read excel file', () => {
cy.task('readXlsx', { file: 'my-excel.xlsx', sheet: "Sheet1" }).then((rows) => {
expect(rows.length).to.equal(543)
// expect(rows[0]["column name"]).to.equal(11060)
})
})
})

需要安装xlsx

 $ npm install xlsx

创建读取excel函数

Cypress \插件\read-xlsx.js

const fs = require('fs');
const XLSX = require('xlsx');

const read = ({file, sheet}) => {
const buf = fs.readFileSync(file);
const workbook = XLSX.read(buf, { type: 'buffer' });
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
return rows
}

module.exports = {
read,
}

将函数用作 Cypress 任务(插件)

Cypress \插件\index.js

const readXlsx = require('./read-xlsx')

module.exports = (on, config) => {
on('task', {
'readXlsx': readXlsx.read
})
}

关于cypress - 在 Cypress 中读取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61934443/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com