3

I want to read data from a particular cell of my excel sheet and then use that data in my cypress tests. The file name is 'qaautomation.xlsx' and sheet name is 'Input' and I want to read data from cell B2. I have written the following code to access the value

 /// <reference types ="cypress" />
var xlsx = require ("xlsx");
var workbook= xlsx.readFile("qaautomation.xlsx");
var worksheet= workbook.Sheets["Input"];
var cellB2value= 'B2';
var cellB2=worksheet[cellB2value];
var cellB2_value=(cellB2.v);

Code written below is where the value would be used.

        describe('Typing the address', function(){
    
    it ('should open the link', function(){
        cy.visit(cellB2_value) //here comes the value from cell B2
})})

When I run the above code I get the following error

The following error originated from your test code, not from Cypress.

  > _fs.readFileSync is not a function

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Cypress could not associate this error to any specific test.

We dynamically generated a new test to display this failure.

Check your console for the stack trace or click this message to see where it originated from.

Is there any way to remove this error?

Amna
  • 103
  • 2
  • 7
  • Does this answer your question? [JavaScript error: "is not a function"](https://stackoverflow.com/questions/9825071/javascript-error-is-not-a-function) – Liam Sep 09 '20 at 12:49
  • 1
    You will need to create a task which can be called from the test, putting your xlsx handling code into `cypress/plugins/index.js`. See [blog](https://glebbahmutov.com/blog/powerful-cy-task) for a tutorial. –  Sep 09 '20 at 13:22
  • 1
    @eric99 reading the above blog and following this link https://docs.cypress.io/api/commands/task.html#Event, I am able to read my excel file and use directly in my cypress tests. Thank you for the guidance – Amna Sep 11 '20 at 12:17

3 Answers3

2

This is a problem with how the XLSX read function deals with webpack and the browser's security feature to not access the file system directly.

You'll need to read the file via an ajax call and then 'read' it into xlsx that way. There's a demo here:

https://github.com/SheetJS/sheetjs/tree/master/demos/xhr

fetch(url).then(function(res) {
  /* get the data as a Blob */
  if(!res.ok) throw new Error("fetch failed");
  return res.arrayBuffer();
}).then(function(ab) {
  /* parse the data when it is received */
  var data = new Uint8Array(ab);
  var workbook = XLSX.read(data, {type:"array"});

  /* DO SOMETHING WITH workbook HERE */
});
GrahamJRoy
  • 1,603
  • 5
  • 26
  • 56
  • I have tried the above solution but now it gives me 'Cannot read property 'B2' of undefined' error – Amna Sep 09 '20 at 08:42
  • looks like it hasn't opened the file Is the path correct? In your example the file would need to be in the same folder as the code looking for it – GrahamJRoy Sep 09 '20 at 09:00
  • My test file 'browser.spec.js' and 'qaautomation.xlsx' file both are in integration folder which is inside the cypress folder. – Amna Sep 09 '20 at 09:06
  • looks like the 'read' function only supports a file stream. https://github.com/SheetJS/sheetjs#parsing-workbooks can you load the data first from an ajax call and then pass it to the read method? – GrahamJRoy Sep 09 '20 at 09:13
  • 1
    I have xlsx file in my folder and I don't need to download it. As I don't have any idea about ajax call, can you clarify that why I have to read xlsx file via ajax call? I'm reading the file in the node environment and tests are in cypress. Basically I need to know how to run node code from cypress tests. – Amna Sep 10 '20 at 07:10
1

Well.. if you are using a (relatively) newer version of cypress there is a dedicated API just for that: https://docs.cypress.io/api/commands/writefile#Examples

From the docs:

cy.writeFile('path/to/message.txt', 'Hello World')
cy.readFile('path/to/message.txt').then((text) => {
  expect(text).to.equal('Hello World') // true
})

yay!

ymz
  • 6,602
  • 1
  • 20
  • 39
-4

move the functionality causing that into cypress/support/ such as in commands.js

TarangP
  • 2,711
  • 5
  • 20
  • 41