How do I parameterize the baseUrl property of the protractor config file
Asked Answered
P

4

20

I need to run my protractor tests in different contexts with different baseUrls in the config files. I don't want to use separate config files for each situation since that is more difficult to maintain. Rather, I want to pass the base url in as a command line parameter. Here is what I have tried so far:

The protractor.conf.js:

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

And to invoke protractor:

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

This does not work since it seems like the browser instance is already configured before onPrepare is called.

Similarly, I have tried this:

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

But this doesn't work either since it seems like the browser instance is not available when the config is being generated.

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

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

Petrosal answered 15/11, 2014 at 21:37 Comment(0)
P
25

Seems like this is already possible, but the documentation is spotty in this area. Looking at the code, however, protractor does support a number of seemingly undocumented command line arguments.

So, running something like this will work:

protractor --baseUrl='http://some.server.com' my.conf.js
Petrosal answered 17/11, 2014 at 3:44 Comment(4)
Thanks for sharing. You should make a pull request to add them to the docs if you find it relevant.Gordan
Yes, I'll consider that.Petrosal
For anyone else if you need to access it in the onPrepare block as OP asked for, can do so via the following code github.com/angular/protractor/blob/1.7.0/docs/… and use config.baseUrl.Primalia
The current syntax for protractor ~4.0.9 seems to be: protractor --baseUrl http://some.server.com/ my.conf.js. I was getting an error using --baseUrl='etc'.Margetts
M
3

The other option is to use gruntfile.js and have it call the protractor config 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']);
};

the Protractor config 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: '[email protected]',
        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 default url http://localhost:6034

grunt protractor

//To run with any other url

grunt protractor --baseUrl:"http://dev.abc.com/"
Metalliferous answered 8/8, 2016 at 10:16 Comment(0)
V
1

I know, old one. but if anyone is still looking for a way to define a url based on capability (I had to do this because Ionic 5 will run in browser on port 8100, but in the app - unchangable - without port declaration on port 80, I use Appium)

add a baseUrl parameter inside your capability declaration.

{
    browserName: 'chrome',
    baseUrl: 'http://localhost:8100' //not required but as example
}

{
    ...
    app: 'path to app.apk',
    baseUrl: 'http://localhost'
    ... 
}

and then configure your onPrepare method as follows.

 async onPrepare() {
    const config = await browser.getProcessedConfig();

    if(config.capabilities.hasOwnProperty('baseUrl')) {
        browser.baseUrl = config.capabilities.baseUrl;
    }
}

OnPrepare runs for each capability you define in your multiCapabilities array. the getProcessedConfig returns the config as you defined it, with the addition of the current capability. Since that method returns a promise, I use async/await for readability.

This way, you can have multiple capabilities running, with each different a different host.

Vacillatory answered 24/4, 2020 at 12:38 Comment(0)
M
0

Base url should be declared baseUrl: "", in config.ts

I am using cucumber hooks and the below code is added in hooks file to pass the required url based upon the environments

 if(browser.params.baseUrl==="QA"){
     console.log("Hello QA")
     await browser.get("https://www.google.com");   
 } else {
     console.log("Hi Dev")
     await browser.get("https://www.gmail.com");
 }

run the tests using protractor command

protractor --params.baseUrl 'QA' typeScript/config/config.js --cucumberOpts.tags="@CucumberScenario"

   
Meseems answered 15/1, 2021 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.