Ivan Markov
Ivan Markov

Reputation: 139

Cypress Data Driven Testing with Excel

I was following this tutorial to read testing data from Excel files with Cypress. The tutorial is wonderful and fully explains everything. There is a git archive with the code too.

The error that I am facing is related to TypeScript. I am using TS in my Cypress project. Screenshot of the issue: enter image description here

let rowsLenght;


describe('The example shows how to use Data Driven Testing using Excel file.', () => {
    before(() => {
        cy.task('readXlsx', { file: 'cypress/fixtures/excelData.xlsx', sheet: "Sheet1" }).then((rows) => {
            rowsLenght = rows.length;
            cy.writeFile("cypress/fixtures/xlsxData.json", { rows })
        })
        cy.visit(Cypress.config('baseUrl'));
    })


    it("example shows how to use data from Excel file.", () => {
        cy.fixture('xlsxData').then((data) => {
            for (let i = 0; i < rowsLenght; i++) {
                cy.get('#username').type(data.rows[i].testData1);
            }
        })
    });
});

When I try to execute the test - everything works. enter image description here

If I rename the file extension from "ts" to "js" - the error is gone. enter image description here

Upvotes: 0

Views: 2201

Answers (1)

Ivan Markov
Ivan Markov

Reputation: 139

I found two solutions.

  1. The first solution is to use the // @ts-ignore comment, to ignore the compiler alert.

enter image description here

let rowsLength;

describe('The example shows how to use Data Driven Testing using Excel file.', () => {
    before(() => {
        cy.task('readXlsx', { file: 'cypress/fixtures/excelData.xlsx', sheet: "Sheet1" }).then((rows) => {
            // @ts-ignore
            rowsLength = rows.length;
            cy.writeFile("cypress/fixtures/xlsxData.json", { rows })
        })
        cy.visit(Cypress.config('baseUrl'));
    })


    it("example shows how to use data from Excel file.", () => {
        cy.fixture('xlsxData').then((data) => {
            for (let i = 0; i < rowsLenght; i++) {
                cy.get('#username').type(data.rows[i].testData1);
            }
        })
    });
});
  1. The second solution is to cast the variable to any, because the variable is "unknown".

enter image description here

let rowsLength:

describe('The example shows how to use Data Driven Testing using Excel file.', () => {
    before(() => {
        cy.task('readXlsx', { file: 'cypress/fixtures/excelData.xlsx', sheet: "Sheet1" }).then((rows: any) => {
            rowsLength = rows.length;
            cy.writeFile("cypress/fixtures/xlsxData.json", { rows })
        })
        cy.visit(Cypress.config('baseUrl'));
    })


    it("example shows how to use data from Excel file.", () => {
        cy.fixture('xlsxData').then((data) => {
            for (let i = 0; i < rowsLenght; i++) {
                cy.get('#username').type(data.rows[i].testData1);
            }
        })
    });
});

Upvotes: 1

Related Questions