0

Am trying to parametrize capabilities using an excel. Am using npm exceljs package for excel reading and writing. I will give the sample code.

//It will read the value from excel

    this.getValueFromExcel = function getValueFromExcel(cellAddress) {
      return  workbook.xlsx.readFile('data.xlsx').then(function () {
            var worksheet = workbook.getWorksheet(sheetName);
            var excelData = worksheet.getCell(cellAddress).value.toString();
            console.log(excelData);
            return excelData;
        })
    }

var browservalue = this.getvalueFromExcel(cellAddress);

//if the browservalue is Chrome expect to be execute this one
var Chrome = {    
     browserName: 'chrome',
    'chromeOptions': {
        'args': ["--disable-infobars"]
    }  
};

//and browservalue is using here
exports.config = {

capabilities: browserValue,

}

Am not sure what am doing wrong, data is fetching from excel because the print statement was working fine. But am getting an error --\ '[15:53:43] E/launcher - Error: TypeError: Target browser must be a string, but i s ; did you forget to call forBrowser()? '

Am not sure is it possible or not , or something am doing wrong?

Am already gone through the link Parametrize protractor-config file , and How can I use command line arguments in Angularjs Protractor?

Basil
  • 77
  • 2
  • 9
  • Your problem is you're trying to read the string "Chrome" from excel and expecting that to match the variable "Chrome" that contains the browser capabilities. What is effectively happening is you're ending up with a piece of code in your config that simply reads `capabilities: "Chrome"` and that's what protractor is complaining about. – M. Hudson Jul 12 '17 at 13:19
  • But it is working fine when am giving directly- var browservalue = Chrome; Then why not using excel? – Basil Jul 13 '17 at 05:28

1 Answers1

0

As I mentioned in the comments, you are trying to pass a string into the capabilities option when it's expecting a variable name. The reason browservalue = Chrome works is because here you ARE passing in the variable name, rather than the string. It's a subtle difference.

Regardless, there is a way of doing what you want to do.

Try this:

var browserValue = this.getvalueFromExcel(cellAddress);

var Chrome = {    
     browserName: 'chrome',
    'chromeOptions': {
        'args': ["--disable-infobars"]
    }  
};

exports.config = {  
    capabilities: eval(browserValue),    
}

Using eval() will interpret a string you pass to it as code and should therefore evaluate the string "Chrome" as the variable name.

You should not use eval() unless completely necessary as any string it receives it will try and execute as javascript code, which could be a security issue. But in this instance I don't believe there's another way.

M. Hudson
  • 889
  • 5
  • 11
  • Thanks for your effort Hudson but unfortunately it is also not working. Am getting the same error – Basil Jul 19 '17 at 06:22
  • Can you use `console.log` to output what you are getting from `this.getvalueFromExcel(cellAddress)` ? – M. Hudson Jul 19 '17 at 08:35
  • Am getting the exact value which is in excel ,now it is - Chrome. – Basil Jul 20 '17 at 08:54
  • I just noticed that in your code, you have both "browservalue" and "browserValue" - it's an error I inadvertantly copied into my own code (now corrected). Can you make sure you are using the same variable name in both places in your actual implementation? – M. Hudson Jul 20 '17 at 09:44
  • Am using the same variable in actual implementation – Basil Jul 24 '17 at 06:25