Reputation: 197
I am working on a task of automation testing and I need to access an excel file to get the input data.
I need help on how to access specific cell values from a local excel file using Javascript.
I have an excel file in my local storage, which has the word 'After Life' in its cell 'B2'.
Currently, I have made two functions: one for navigation to the desired page and the other for search. But, as I don't know how to import data from a local excel file, I typed the string argument 'After Life' manually for the search function.
Upvotes: 1
Views: 662
Reputation: 2894
I'd recommend importing the file as .csv and creating a ReadStream in conjunction with csv-parser
to consume and parse it in your E2E-Test.
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
const findByKey = (key) => {
return new Promise ((resolve, reject) => {
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
const searchString = results.forEach((item) => {
return item[key];
});
if (item.key && item.key.length > 0) {
resolve(item.key);
} else {
reject();
}
});
})
}
This way, you can then find the String in your Search function, if you use async/await
.
export async function search (searchText) {
const searchString = await findByKey(searchText);
cy.get('.gLFyf').type(searchText + '{enter}');
}
and then use it like this:
it('should search After Life', () => {
search('Search String 2');
});
Upvotes: 1