【问题标题】:Unable to pass arguments from command to grunt file无法将参数从命令传递到 grunt 文件
【发布时间】:2014-09-25 05:12:57
【问题描述】:

我将下面的代码用于量角器配置文件中使用的参数

protractor: {

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

            }
        }
      },

并在量角器配置文件中检索为 browser.params.user,browser.params.password

这些是工作文件。 我想从命令更改用户和密码值。 如何更改值?

【问题讨论】:

    标签: javascript gruntjs jasmine protractor


    【解决方案1】:

    这是一个简单的解决方法:

    当您将参数传递给您的 grunt 任务时:

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

    它可以作为

    grunt.option('user')
    

    然后您可以使用以下命令编辑配置中的值:

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

    不确定这是最好的方法,但对我来说效果很好。 另请注意,我们正在包装量角器任务,而不是立即调用它

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        在下面的代码中,我注册了一个任务“myprotractor”,任务之后作为参数的任何内容都将作为参数进入匿名函数:

        咕哝我的量角器:开发:密码

        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'
        
                ]);
            });
        };
        

        如果您需要,您可以为量角器配置 2 个目标,具有一些通用配置,并且根据您希望从 cmd 还是从 config 设置 args。

        咕哝着我的量角器: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
        
                ]);
            });
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-20
          • 1970-01-01
          • 2023-02-10
          相关资源
          最近更新 更多