1

How to create parameters in my scripts, for example instead of put the url in the script, put a variable and read an excel file or data base for example.

i have this .url("https://www.abcdef.com/#admin")

i want somethink like this .url(url readed from excel or data base)

Luis Coronel
  • 13
  • 1
  • 4

4 Answers4

1

I'm also new to node/nightwatch, so there may be a better way, but this is a working solution.

var xlsx = require('xlsx');
var fs = require('fs');
if(typeof require !== 'undefined') XLSX = require('xlsx');
var workbook = XLSX.readFile('MyExcelDoc.xlsx');
var sheetnamelist = workbook.SheetNames;

module.exports = {
'MyTest' : function (browser) {
        //iterates through excel sheet testing each URL
        sheetnamelist.forEach(function(y) {
            var worksheet = workbook.Sheets[y];
            var z;
            for (z in worksheet) {
                if(z[0] === '!') continue;
                var url = worksheet[z].v;
                browser
                    .url(url);
                    //do something at your url//
            }
        });
    browser.end()
    }       
};

ps, I hope the bracketing and stuff is right. I copied my code and deleted all the irrelevant things, and hopefully I also deleted the right amount of brackets.

SbnSeebee
  • 64
  • 8
Sarah
  • 348
  • 1
  • 2
  • 12
0

Have you tried fs.readFile()? If your url is in a file, you can just read the url or list of urls from a file. If you have an excel spreadsheet, you may want to convert it to a .csv file, or find some sort of node package that can read excel files.

If your urls are in a database, then you'll have to use the proper database connection string to access the database before you run your test. I'm admittedly new to node, so I don't know the ins and outs of database access, but it's not that much different from other server based dev environments.

flipvinyl
  • 41
  • 1
  • 1
  • 3
0

The answer Sarah gave worked great!
If you are planning on using js-xlsx, I recommend parsing the rows to JSON which makes them more readable/useable.

See this SO answer

Community
  • 1
  • 1
SbnSeebee
  • 64
  • 8
0
var xlsx = require('node-xlsx');
var path = require('path');

const workSheetsFromFile = xlsx.parse(path.join(__dirname,`../orgData.xlsx`));

console.log(workSheetsFromFile);

module.exports= workSheetsFromFile[0].data[0];
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 2
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Apr 05 '18 at 09:31