OmPrakash
OmPrakash

Reputation: 41

Unable to pass arguments from command to grunt file

I am using the code bellow for arguments which are using in protractor configuration file

protractor: {

      options: {
        keepAlive: true,
        configFile: "test/config.js",
        args:{
            params:{
                user:"user1",
                password:"password1"

            }
        }
      },

and retrieving in protractor conf file as browser.params.user,browser.params.password

These are working files. I want to change the user and password values from command. How to change the values?

Upvotes: 3

Views: 1461

Answers (3)

mkbrv
mkbrv

Reputation: 407

In the code below I register a task "myprotractor" and whatever comes after the task as argument will go as parameter into the anonymous function:

grunt myprotractor:dev:pwd

module.exports = function(grunt) {
    grunt.registerTask('myprotractor', function(user, pwd) {
        console.log(user + pwd);
        grunt.config('protractor', {
            options: {
                keepAlive: true,
                configFile: "test/config.js",
                args: {
                    params: {
                        user: user,
                        password: pwd

                    }
                }
            }
        });

        //here I am running the task
        grunt.task.run([
            'protractor'

        ]);
    });
};

If you need you can configure 2 targets for protractor, having some common configuration and the args being set depending if you wish them from cmd or from config.

grunt myprotractor:cmd:dev:pwd

module.exports = function(grunt) {
    grunt.registerTask('myprotractor', function(target, user, pwd) {
        console.log(user + pwd);
        grunt.config('protractor', {
            options: {
                keepAlive: true,
                configFile: "test/config.js"
            },
            cmd: {
                options: {
                    args: {
                        params: {
                            user: user,
                            password: pwd

                        }
                    }
                }
            },
            config: {}
        });


        //here I am running the task with a target
        grunt.task.run([
            'protractor:' + target

        ]);
    });
};

Upvotes: 0

Hasan
Hasan

Reputation: 188

how to fetch --user argument in protractor Specs?

Upvotes: 0

Alex Sayegh
Alex Sayegh

Reputation: 243

This is a simple work around:

When you pass a parameter to your grunt task:

grunt e2e --user=alex --password=password

it's available as

grunt.option('user')

You can then edit the values you have in the config using:

var protConfig = grunt.config.get('protractor');
protConfig.options['someKey']=newValue;
grunt.config('protractor', protConfig);
grunt.task.run('protractor');

Not sure this is the best way to go about it, but it's working fine for me. Also notice that we're wrapping the protractor task rather than calling it right away

Upvotes: 2

Related Questions