0

I am trying to get command line argument values using NPM package "yargs" instead of retrieving the values from config.ts file.

Example: My config.ts file looks like

export const config: Config = {  
 username: process.argv[3],                                                                     
  password: process.argv[4]                       
}

And in my package.json file I already defined my scripts for "test": "./node_modules/.bin/protractor typeScript/config/config.js",

In my testcase i am retrieving values as "config.username" & "config.password"

So when i execute npm test --username1 --password1 , my testcase which is executing is picking values from config.ts file and not the values which i passed "username1" and "password1" from command line. Can someone please help me in picking the values from command line and not from the config.ts file.

Thanks in advance.. !!!

Nan Du
  • 3
  • 1
  • Can you https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script – Nithish Nov 21 '19 at 12:24

1 Answers1

0

1.

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

BUT! These variables are not available in your config and only in specs.

2.

If you need variables to be reused in config (conditional suites etc) The way I'm doing this is following

Lets assume you have a command to start your tests

protractor config.js --baseUrl="www.example.com"

you can declare environment variables BEFORE that command like so

INFO=something PARAM=parameter protractor config.js --baseUrl="www.example.com"

And then in your protractor config you can refer to those variables by typing

console.log(process.env.INFO);
console.log(process.env.PARAM);

Note that you don't even need additional packages for this approach

Community
  • 1
  • 1
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • I used solution1 and did some changes along with what is suggested like in config.ts file i added process.argv[3] for baseUrl and when i run the test as "./node_modules/.bin/protractor typeScript/config/config.js --http://www.google.com" , script is taking value along with -- and if i dont give -- like "./node_modules/.bin/protractor typeScript/config/config.js http://www.google.com" and run the test then it is throwing error :Error: more than one config file specified. Can you please help me if you know how can i resolve this. – Nan Du Nov 22 '19 at 08:03
  • `.replace("--","")`? – Sergey Pleshakov Nov 22 '19 at 14:50
  • Thanks..!! It resolved my issue , but by any chance do you any have idea on how i can pass a value with double quotes like "username" from command line. – Nan Du Nov 28 '19 at 09:50
  • probably you just need to escape it `"\"your value\""` – Sergey Pleshakov Nov 28 '19 at 13:29
  • I tried that also but it is taking value as \\@Test\\ when I passed it as -\"@Test\" – Nan Du Nov 29 '19 at 09:33