簡體   English   中英

無法將參數從命令傳遞到grunt文件

[英]Unable to pass arguments from command to grunt file

我正在使用下面的代碼,用於在量角器配置文件中使用的參數

protractor: {

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

            }
        }
      },

並在量角器conf文件中檢索為browser.params.user,browser.params.password

這些是工作文件。 我想從命令更改用戶和密碼值。 如何更改值?

這是一個簡單的解決方法:

將參數傳遞給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');

不確定這是最好的方法,但它對我來說很好。 還要注意我們正在包裝量角器任務而不是立即調用它

如何在量角器規范中獲取--user參數?

在下面的代碼中,我注冊了一個任務“myprotractor”,任務后面的任何參數將作為參數進入匿名函數:

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'

        ]);
    });
};

如果需要,可以為量角器配置2個目標,具有一些通用配置,並且根據您希望它們來自cmd或配置來設置args。

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