0

I want to save console output to excel, not sure how to do that in java-scrip.

console.log(result.value);

I want my result.value to be saved in excel through java script/Nightwatch.

This saved data in excel is then need to modified ( part of text need to be used) & used by different program.

3 Answers3

1

You can use node in nightwatch, as a matter of fact.

You can do what you have described using a custom command. In the custom_commands folder create say, helperFunctions.js, which looks like this:

    var fs = require('fs'); // NPM to create a file

    module.exports = {
       saveToFile : function(client, data, path){
            this.fs = fs;
            buffer = new Buffer(data);
            // the  path here is the directory to your xls file and the file name
            fs.open(path, 'w', function(err, fd) {
                if (err) {
                    throw 'error opening file: ' + err;
                }
                // write the data
                fs.write(fd, buffer, 0, buffer.length, null, function(err){
                    if (err) throw 'error writing file: ' + err;
                    return fs.close(fd, function() {
                       // great place to put a console.log to indicate success
                       console.log('File created / updated ' + filename)
                    })
                });
            })
       }
    };

Then you can call save to file from your regular test suite like so:

"Save excel data to a file for later use": function(client) {
    client
       // where data is a variable with whatever data you want in the file.
       this.helperFunctions.saveToFile(client, data, "conf/MyExcelData.xlsx")
},

You may need to play around with this a bit to get it to do what you want, but rest assured, I have done it with text and js files and I know of no reason this cannot be done with any file data that will fit into a buffer.

QualiT
  • 1,934
  • 2
  • 18
  • 37
0

Not sure what your code looks like, but you could use:

node myApp.js > consoleLogOutput.xlsx

This will take your console.log ("Hello, world!") output and save it to the directory your myApp.js script is running from.

sudodashell
  • 121
  • 4
  • 12
-2

Unfortunately, vanilla JavaScript can't write data to files. Nightwatch.js as far as I am concerned is used for testing so it won't help you either.

If you want to write your data to a file use Node.js.

Andrew
  • 449
  • 3
  • 7