Reputation: 139
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:
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.
If I rename the file extension from "ts" to "js" - the error is gone.
Upvotes: 0
Views: 2201
Reputation: 139
I found two solutions.
// @ts-ignore
comment, to ignore the compiler alert.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);
}
})
});
});
any
, because the variable is "unknown".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