0

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

1 Answer 1

1

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);
            }
        })
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.