Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
Cannot read properties of undefined looping through XLSX file in Cypress test

I am trying to convert values from an XLSX file into a JSON file in a Cypress test.

I have copied code form this blog exactly, but when I hit the below line:

console.log(jsonData[index].data)

I get this error message:

Cannot read properties of undefined (reading '1')

Here is the code in my spec.js file:

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] })
            }
        })
    })
})

And here is where my parseXlsx function is defined:

Plguins/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);
        }
      });
    }
  });
}

My cypress/fixtures/excelData.xlsx file looks like this:

USERNAME PASSWORD

Username1 Password1

Username2 Password2

Can someone please tell me how to resolve this error?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I managed to find a solution:

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] })
            }
        })
    })
})  
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda