SheetJS包可用于在用base64
编码读取后解析XLSX文件
在下面的示例中,我使用了一个只包含文本的小工作表
在读取该文件之后,我将其转换为HTML,以便可以将其挂载到Cypress测试页面中.
测试运行器显示数据,并且可以像网页一样进行查询.
const XLSX = require('xlsx')
it('reads and tests an XL file', () => {
cy.readFile('cypress/downloads/file_example_XLS_10.xlsx', 'base64')
.then(file => {
// parse to HTML
const data = XLSX.read(file)
const worksheet = data.Sheets['Sheet1']
const html = XLSX.utils.sheet_to_html(worksheet)
// mount to test page
const worksheetAsTable = Cypress.$(html)
Cypress.$('body').empty()
Cypress.$('body').append(worksheetAsTable)
})
// Now test the HTML table with Cypress commands
cy.get('table tr:nth-child(3) td:nth-child(3)')
.invoke('text')
.should('eq', 'Hashimoto')
// Or simply assert the text is present
cy.get('body').should('contain', 'Hashimoto')
})