File conversion is one method we could use to read data from excel files. Due to Cypress’s lack of support.xlsx file. The only format supported by Cypress is JSON, thus we would need to convert it. In other words, we will convert the Excel.xlsx file to a JSON file. And using that JSON file, we’ll be able to extract the info we require.
To fulfill our use case, we will require an additional plugin. Run the following command at the project root level to accomplish this.
npm install node--xlsx --save-dev
We would then need to add a task to our e2e.js file located in the plugin folder. e2e.js files are mostly used to manage plugins and are found in plugin directories.
In the e2e.js file, we would need to define a job for reading the excel file.
In order to parse the xlsx file, a job has now been started.
The only thing left to do is convert the data to JSON and save it in a fresh JSON file below is the code to convert it.
In this case, we used the parseXlsx function, in which the e2e.js file already
contained an event. Then, after iterating each element on the Excel sheet, we assigned each one to the template we created in the writeFile () method.
To create a new JSON file and parse the extracted data into it, use the WriteFile () method. That’s all there is to it; the xlsx file has been successfully converted to a JSON file. We can now use the data in our test due to fixtures.
The command.js file in the support folder and the following code must now use, as shown so that it won’t make any errors.
Now we can run the script can see the JSON data on the console.
This is the whole script where we are driving data from excel for login.
Source Link:- https://www.devstringx.com/read-write-excel-data-in-cypress
Comments