Estoy tratando de convertir valores de un archivo XLSX en un archivo JSON en una prueba de Cypress.
Copié el código de este blog exactamente, pero cuando llegué a la siguiente línea:
console.log(jsonData[index].data)Recibo este mensaje de error:
No se pueden leer las propiedades de undefined (leyendo '1')
Aquí está el código en mi archivo spec.js :
describe('convert data to Json', () => { it('read data from xcel', () => { cy.parseXlsx('cypress/fixtures/excelData.xlsx').then((jsonData) => { const rowLength = Cypress.$(jsonData[0].data).length for (let index = 0; index < rowLength; index++) { var jsonData = jsonData[index].data console.log(jsonData[index].data) cy.writeFile("cypress/fixtures/xlsxData.json", { username: jsonData[0][0], password: jsonData[0][1] }) } }) }) }) Y aquí es donde se define mi función parseXlsx :
Complementos/index.js :
const xlsx = require('node-xlsx').default; const fs = require('fs'); // for file const path = require('path'); // for file path module.exports = (on, config) => { on('task', { parseXlsx({ filePath }) { return new Promise((resolve, reject) => { try { const jsonData = xlsx.parse(fs.readFileSync(filePath)); resolve(jsonData); } catch (e) { reject(e); } }); } }); } Mi archivo cypress/fixtures/excelData.xlsx tiene este aspecto:
USUARIO CONTRASEÑA
Nombre de usuario1 Contraseña1
Nombre de usuario2 Contraseña2
¿Alguien puede decirme cómo resolver este error?
Logré encontrar una solución:
describe('convert data to Json', () => { it('read data from xcel', () => { cy.parseXlsx('cypress/fixtures/excelData.xlsx').then((jsonData) => { const rowLength = Cypress.$(jsonData[0].data).length for (let i = 0; i < rowLength; i++) { cy.log('Username: ' + jsonData[0].data[i][0]); cy.log('Password: ' + jsonData[0].data[i][1]); cy.writeFile("cypress/fixtures/xlsxData.json", { usernameValue: jsonData[0].data[i][0], passwordValue: jsonData[0].data[i][1] }) } }) }) })