williamtell
williamtell

Reputation: 301

Trouble passing Jenkins parameters to run Protractor scripts

I am currently using 2 config files to run my Protractor scripts using Jenkins.

devConfig.ts and prodConfig.ts

These have the dev creds and URL and the prod creds and URL.

I have two Jenkins jobs that run different commands

npm run tsc && protractor tmp/devConfig.js --suite devRegression
npm run tsc && protractor tmp/devConfig.js --suite prodRegression

Instead of having two config files, how is it possible to do in one? By passing params for URL, Creds, Suite and Browser?

I was able to setup on Jenkins:

enter image description here

and this leads to

enter image description here

But I am not able to pass them back to the protractor scripts. Is there a straightforward way to construct these parameters and pass them on to protractor?

Upvotes: 1

Views: 513

Answers (1)

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

For protractor side check out this page

Per its content, having this in your conf.js:

module.exports = {
  params: {
    login: {
      email: 'default',
      password: 'default'
    }
  },
    //  * other config options *
}

you can pass any parameter to it in CMD as follows:

protractor --baseUrl='http://some.server.com' conf.js [email protected] 
--parameters.login.password=foobar

so you end up having this in your specs:

describe('describe some test', function() {
  it('describe some step', function() {
    browser.get(browser.baseUrl);
    $('.email').sendKeys(browser.params.login.email);
    $('.password').sendKeys(browser.params.login.password);
  });
});

For Jenkins just construct the command as follows:

protractor --baseUrl=${url} conf.js --parameters.login.email=${email}
--parameters.login.password=${password}

Another way if you want to just pass one parameter is have object in your config.js with mapping of all related params like this:

let param_mapping = {
    prod: {
        url: "https://prod.app.com",
        email: "[email protected]",
        password: "Test1234"
    },
    dev: {
        url: "https://dev.app.com",
        email: "[email protected]",
        password: "Test1234"
    },
    stage: {
        url: "https://stage.app.com",
        email: "[email protected]",
        password: "Test1234"
    }
};

let parameters = param_mapping[process.ENV.CUSTOM_ENV];

exports.config = {
    baseUrl: parameters.url,
    params: parameters,
    // ...
};

and then start your process with an environment variable:

CUSTOM_ENV=dev protractor protractor.conf.js

Please note, I haven't tested this particular code now, but I did test the logic a little while ago, so this can be your approach

Upvotes: 2

Related Questions