how can Parametrize protractor-config file for dif

2020-06-30 04:21发布

I am running my Protractor Test from command npm run e2e

I want a way so that if I pass npm run e2e firefox then my test will get executed on Firefox browser.

Or if I run npm run e2e chrome then it should run on chrome

if I pass both npm run e2e firefox chrome then my test should run on both the browser in parallel.

Is it possible to parametrize protractor config file?

Similarly if I can pass test suite name via command and it should execute only tests under that particular test suite.

Here is my config file and this is what I want to achieve:

`//var HtmlReporter = require('protractor-html-screenshot-reporter');

exports.config = {

allScriptsTimeout: 30000,
//Add parameters for browser names
params:{
  pass: {
      browserName : 'chrome',
      testSuitName : 'e2e/TestSuites/_BVT/*.js',
  }
 },  
suites: {    
//Define here List of Sanity Test Scenarios:
  BVT : testSuitName,   
}, 
// configure multiple browsers to run tests
multiCapabilities: [
 shardTestFiles: true,
 maxInstances: 2   
{'browserName': browserName}
],
baseUrl: 'http://mytestUrl/',
 framework: 'jasmine2',
jasmineNodeOpts: {
 defaultTimeoutInterval: 30000
},  
onPrepare: function() {  
var jasmineReporters = require('jasmine-reporters');
browser.driver.manage().window().maximize();
return browser.getProcessedConfig().then(function(config) {   
  var browserName = config.capabilities.browserName;
  var junitReporter = new jasmineReporters.JUnitXmlReporter({
         consolidateAll: true,
         savePath: 'tests/test-results',            
         filePrefix: browserName + '-xmloutput',
         modifySuiteName: function(generatedSuiteName, suite) {             
             return browserName + '.' + generatedSuiteName;
         }              
     });
     jasmine.getEnv().addReporter(junitReporter);
  });    
}, 
resultJsonOutputFile: 'tests/test-results/output.json'    
};`

Would appreciate any help on this.
Thanks in advance.

标签: protractor
3条回答
家丑人穷心不美
2楼-- · 2020-06-30 04:57

I know this post is a bit old now, but this solution may help people having a similar problem.

Use multiple config files, one for each browser type, setup a base config file, and require that into each of the other config files, and then create an npm script for each config file. No need for parameters and everything stacks nicely.

so the base config (called something like "protractor_base.js")would look something like:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  rootElement: '[ng-app]',

  allScriptsTimeout: 60000,

  framework: 'jasmine',

  specs: ['example-spec.js']
};

And then your other configs (ie "protractor_chrome.conf.js") would look something like:

protractor = require('./protractor_base.js');
var config = protractor.config;

config.capabilities: {
  'browserName': 'chrome'
};


exports.config = config;

You can then specify a multi browser config, just a chrome one, etc.

查看更多
姐就是有狂的资本
3楼-- · 2020-06-30 05:04
--capabilities.chromeOptions.args=headless --capabilities.chromeOptions.args=disable-gpu --capabilities.chromeOptions.args=window-size=1200,600

The above code should work for you.

查看更多
冷血范
4楼-- · 2020-06-30 05:14

I was looking for passing parameters from command line to protractor config file and found a way to do this:

npm run e2e -- --baseUrl=http://testurl:8080 --suite=suite_name_defined_in_config --capabilities.browserName=browser_Name

where my npm package.json :

 "e2e": "protractor tests/protractor-conf.js",

and config file contains :

suites: {
    BVT: 'e2e/TestSuites/_BVT/*.js',
    Full: 'e2e/TestSuites/Full/**/*.js',      
},
capabilities: {
  'browserName': 'chrome'
},
baseUrl: 'http://localhost:8080/',  
查看更多
登录 后发表回答