1

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?

williamtell
  • 301
  • 5
  • 17
  • why you start thinking about creating one config file? Two configs is ordinary thing. Do not need to make system more complex. – Oleksii Oct 24 '19 at 18:06
  • @oleksii disagree, my rule - if I have the same code twice anywhere in the project, I'm doing it wrong. Result - one config file for 14 apps, 4 envs each, one data file for each env - 14*4, all possibilities of combinations of browsers and OSs, optional video recording etc – Sergey Pleshakov Oct 24 '19 at 19:11
  • and I would not consider the system more complex in this case, rather more flexible, which is a move forward for any project – Sergey Pleshakov Oct 24 '19 at 19:28

1 Answers1

2

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 --parameters.login.email=example@gmail.com 
--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: "prod@gmail.com",
        password: "Test1234"
    },
    dev: {
        url: "https://dev.app.com",
        email: "dev@gmail.com",
        password: "Test1234"
    },
    stage: {
        url: "https://stage.app.com",
        email: "stage@gmail.com",
        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

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40