简体   繁体   English

无法将参数从命令传递到grunt文件

[英]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 并在量角器conf文件中检索为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? 如何更改值?

This is a simple work around: 这是一个简单的解决方法:

When you pass a parameter to your grunt task: 将参数传递给grunt任务时:

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 还要注意我们正在包装量角器任务而不是立即调用它

如何在量角器规范中获取--user参数?

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: 在下面的代码中,我注册了一个任务“myprotractor”,任务后面的任何参数将作为参数进入匿名函数:

grunt myprotractor:dev:pwd 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. 如果需要,可以为量角器配置2个目标,具有一些通用配置,并且根据您希望它们来自cmd或配置来设置args。

grunt myprotractor:cmd:dev:pwd 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

        ]);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM