Handle Excel file with Cypress: "path" argument must be of type string or an instance of Buffer or URL

Viewed 534

I have followed the instructions of [https://blog.knoldus.com/reading-data-from-excel-file-cypress/][1]

... and some other suggestions.

But I have not getting anywhere close to read/"change to json" an Excel file.

The error looks like this in cypress/support/cy-commands.ts:134:44:

cy.task('parseXlsx') failed with the following error:

> The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of ObjectLearn more

and

> 134 | Cypress.Commands.add('parseXlsx', () => cy.task('parseXlsx', { filePath: InputFile }));

The cy-commands looks like this:

import { InputFile } from './spa/Component-API';
Cypress.Commands.add('parseXlsx', () => cy.task('parseXlsx', { filePath: InputFile }));

In the Component-API InputFile is defined as string.

Tn the plugin/index.js file

import * as fs from 'fs';
import * as path from 'path';
import * as xlsx from 'node-xlsx';
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);
        }
      });
    },
  });

And the test itself (not finished):

validateExcelFileNotEmpty() {
    cy.get('@LASTDOWNLOADEDFILE').then((name) => {
      const fileName = name as unknown as string;
      cy.log(`Validating excel file: ${fileName}`);
      cy.parseXlsx(name as unknown as string).then((jsonData) => {
        const rowLength = Cypress.$(jsonData[0].dataset).length;
      });
    });
  }

Basically all what is described in the link above. I have written other task and they are working, but not this one!

1 Answers

{ filePath: InputFile } is defining an object with a filePath field. But your parseXlsx function is expecting to receive just a plain string. So the correct way to call the parseXlsx task is the following: Cypress.Commands.add('parseXlsx', () => cy.task('parseXlsx', InputFile ));

Besides that, I don't understand why you try to pass an argument to you custom command whereas it accepts none.

Related