Thursday, 17 July 2014

Protractor + grunt + Pass arguments at runtime as param object.

"Protractor ":

  •     Awesome end to end automation testing framework for AngularJS applications and combining powerful tools and technologies such as NodeJS, Selenium, web Driver, Jasmine, Cucumber and Mocha.
  •     It is very simple to learn and develop and also there are lots of debugging option to debug the code easily(like jasmine-reporter, Snapshot when requied like test passed or failed, HTML Reporter with Screen shots).
  •     Protractor allows tests to be organized based on Jasmine, thus allowing you to write both unit and functional tests on Jasmine.
  •     It can runs on real browsers and headless browsers(using saucelabs).
  •     We can use CI tool like Jenkins for doing continues integration nightly build from remote.
  •     Config file has lots of customizations on angular app testing.
  •     Protractor meet the needs of the community that is using AngularJS. 
  •     It runs on top of the Selenium, and thus provides all the benefits and advantages from Selenium.
  •     Grunt is used to automation javascript tasks when performing repetitive tasks.

grunt-Protractor-runner:

    A Grunt plugin for running Protractor runner.
    It requires Grunt ~0.4.1 , Protractor >=0.14.0-0 <1.0.0 Node.js and versions >= 0.8.0
   

Configuring Grunt inside the protractor :

Step : 1   
     you want to install Grunt's command line interface (CLI) globally. CLI loads the local installation of the Grunt library, applies the configuration from your Gruntfile
        sudo npm install -g grunt-cli
Step: 2
    Installing grunt on local system
        sudo npm install grunt --save-dev
   
Step : 3
    Configuring grunt-protractor-runner plugin :
        npm install grunt-protractor-runner --save-dev
   
        This plugin will install protractor module locally as a normal dependency. Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

            grunt.loadNpmTasks('grunt-protractor-runner');
Step: 4
    The Gruntfile.js or Gruntfile.coffee file is a valid JavaScript  file that belongs in the root directory of your project.
            Create a js file and name it Grunt.js.
           
                module.exports = function(grunt) {
                          grunt.initConfig({
                               protractor: {
                                        options: {
                                    keepAlive: true,
                                    singleRun: false,
                                    configFile: "***.js",
                                    template: 'custom.tmpl',
                                    junit: {
                                        path: 'outputdir',
                                        consolidate: true
                                    },
                                     args: {
                                        // Arguments passed to the command
                                         }
                                    },
                                    your_target: {
                                       options: {
                                        args: {
                                        specs: ["config-spec.js"],
                                        },
                                    }
                                    },
                            },
                        })
                    };   

Sending argument as a param object to grunt file and we can access the parameter from config.spec.js file.

Step : 1       
    Config your grunt.js file
    Recieving the parameter object
                var receiveobject = grunt.option('arg1');
Step : 2
    Convert the param-object to json-object to receive it in config-spec.js file.
   
            var jsonString = '{' ;
            jsonString = jsonString + '"arg1": "' + arg1 + '"';
            jsonString = jsonString + '}';
Step : 3   
    To access the json object inside the grunt file, declare it inside the grunt.initConfig

                 grunt.initConfig({
                    // Configuration to be run (and then tested).
                    data: JSON.parse(jsonString),
                    protractor: {
                         your_target: {

                            options: {
                                args: {
                                specs: ["test.js"],
                                params: '<%= data %>'// json object send to conf-spec file like this.
                                },
                            }
                                },
Step: 4   
    Receiving the json object at config.spec file:
        browser.params.arg1;
Step: 5
    Sending the argument to the grunt.js file.
        grunt default --arg1=parametervalue

Concurrently running the tests using grunt:

Step : 1
                protractor: {
                         test1: {
                            options: {
                                args: {
                                specs: ["test1.js"],
                                params: '<%= data %>'// json object send to conf-spec file like this.
                                },
                            }
                                },
                         test2: {
                            options: {
                                args: {
                                specs: ["test2.js"],
                                params: '<%= data %>'// json object send to conf-spec file like this.
                                },
                            }
                                },
                         test3: {
                            options: {
                                args: {
                                specs: ["test3.js"],
                                params: '<%= data %>'// json object send to conf-spec file like this.
                                },
                            }
                                },
                    }

Step: 2
    Register the task
        grunt.registerTask('protractor-task', ['protractor:test1','protractor:test2','protractor:test2']);
Step: 3
    Execute it : grunt portractor-task
       

No comments:

Post a Comment