0

I am Very much new to Protractor . So can any body please help me to get the date from xlsx file to the web apps. Below is the code which I wrote.

   var testData1 = require('path/book1.xlsx');
   var a = element(by.id("Name"));
   var b = element(by.id("Description"));
   a.sendKeys(testData1[0].Name);
   b.sendKeys(testData1[0].Description); 
   browser.sleep(3000);

I am Getting this error`

 Message:
   Failed: Invalid or unexpected token
 Stack:
   D:\Protractor\book1.xlsx:1
   (function (exports, require, module, __filename, __dirname) { PK♥♦¶

this if xlsx file

enter image description here ^`

  • Possible duplicate of [Reading Excel file using node.js](https://stackoverflow.com/questions/28860728/reading-excel-file-using-node-js) – Gunderson Mar 20 '18 at 13:08
  • @Gunderson I have tried the above code and its not working for me Can you please help me out I am very much new to protractor – Unmesh Ghatbandhe Mar 22 '18 at 10:50

1 Answers1

1

Install node-xlsx as project dependency by execute npm install -S node-xlsx

//excelAgent.js

 var xlsx = require('node-xlsx');

 exports.read = function(_file) {
    var xlsObject = xlsx.parse(_file);

    return xlsObject? xlsObject:[];
 }

// test.js

var excelAgent = require('./excelAgent.js');

var excelFile = '';
var data = excelAgent.read(excelFile)[0].data; // first sheet's data
var data = excelAgent.read(excelFile)[1].data; // second sheet's data

// project folder structure:

enter image description here

// folder node_modues structure:

enter image description here

yong
  • 13,357
  • 1
  • 16
  • 27
  • thanks But I am Getting this **Failed: Cannot find module 'node-xlsx'** and on the `_file` I have given the file path am I right . I have Executed the command `npm install -S node-xlsx` – Unmesh Ghatbandhe Mar 22 '18 at 10:48
  • After execute `npm install -S node-xlsx`, can you find folder: `node-xlsx` under folder: `node_modules`? Did `test.js` and `excelAgent.js`has same parent folder as `node_modules`? – yong Mar 22 '18 at 13:30
  • I am getting this error after i have made all the changes you told above **Failed: xlsx is not defined** – Unmesh Ghatbandhe Mar 23 '18 at 11:49
  • Please get demonstrate project I prepared from https://github.com/yongcy/stackoverflow/tree/read-excel – yong Mar 23 '18 at 18:15
  • Thanks Man. its working absolutely fine for me now. Can we save the cell data like **1st column 1st row data** in the other variable. – Unmesh Ghatbandhe Mar 26 '18 at 07:38