How can I parameterize the baseUrl property of the protractor configuration file

I need to run protractor tests in different contexts with different baseUrl in the configuration files. I do not want to use separate configuration files for each situation, as it is more difficult to maintain. Rather, I want to pass the base url as a command line parameter. Here is what I have tried so far:

File protractor.conf.js:

 exports.config = { onPrepare : { ... exports.config.baseUrl = browser.params.baseUrl; ... } } 

And to call the protractor:

 protractor protractor.conf.js --params.baseUrl 'http://some.server.com' 

This does not work, as it seems that the browser instance is already configured before calling onPrepare .

Similarly, I tried this:

 exports.config = { baseUrl : browser.params.baseUrl } 

But this will not work, as it seems that the browser instance is not available when creating the configuration.

It looks like I can use the standard node process.argv to access all the command line arguments, but this seems to contradict the spirit of the protractor.

What is the best way to do what I need to do?

+14
source share
2 answers

This seems to be already possible, but the documentation in this area is spotty. However, looking at the code , the protractor supports several seemingly undocumented command line arguments.

So, work on something like this will work:

 protractor --baseUrl='http://some.server.com' my.conf.js 
+22
source

Another option is to use gruntfile.js and invoke the protractor configuration file.

//gruntfile.js

 module.exports = function (grunt) { grunt.registerTask("default", "", function () { }); //Configure main project settings grunt.initConfig({ //Basic settings and infor about our plugins pkg: grunt.file.readJSON('package.json'), //Name of plugin cssmin: { }, protractor: { options: { configFile: "conf.js", // Default config file keepAlive: true, // If false, the grunt process stops when the test fails. noColor: false, // If true, protractor will not use colors in its output. args: { baseUrl: grunt.option('baseUrl') || 'http://localhost:6034/' } }, your_target: { // Grunt requires at least one target to run so you can simply put 'all: {}' here too. options: { configFile: "conf.js", // Target-specific config file args: { baseUrl: grunt.option('baseUrl') || 'http://localhost:63634/' } } }, }, //uglify uglify: { } }); //Load the plugin grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-protractor-runner'); //Do the Task grunt.registerTask('default', ['cssmin']); }; 

Protractor configuration file: conf.js

 exports.config = { directConnect: true, // Capabilities to be passed to the webdriver instance. capabilities: { 'browserName': 'chrome', 'chromeOptions': { args: ['--no-sandbox'] } }, chromeOnly: true, // Framework to use. Jasmine is recommended. framework: 'jasmine', // Spec patterns are relative to the current working directory when // protractor is called. specs: ['specs/*/*_spec.js'], suites : { abcIdentity : 'specs/abcIdentity/*_spec.js' //picks up all the _spec.js files }, params: { UserName: ' abc@test.com ', Password: '123' }, // Options to be passed to Jasmine. jasmineNodeOpts: { defaultTimeoutInterval: 30000, includeStackTrace: true }, onPrepare: function () { browser.driver.manage().window().maximize(); if (process.env.TEAMCITY_VERSION) { var jasmineReporters = require('jasmine-reporters'); jasmine.getEnv().addReporter(new jasmineReporters.TeamCityReporter()); } } }; 

// To run with the default URL http: // localhost: 6034

 grunt protractor 

// To run with any other URL

 grunt protractor --baseUrl:"http://dev.abc.com/" 
+1
source

Source: https://habr.com/ru/post/987612/


All Articles