Search code examples
intern

How to pass custom argument in inern 4


I have been deeveloping intern automation test from my product. I was using intern version 3 for this. But since intern's version 4 have arrived , I was trying to get familiar with it. Now my question is how to pass custom argument to intern 4. I intern 3 i used to pass it through start command like this

node_modules/intern/runner.js config=/Automation/intern -leaveRemoteOpen arg1=arg1 arg2=arg2

and catching those argument using inter.args.arg1.

But since in intern 4 I'm starting test using npm test command I'm unable to figure out how to pass custom argument and how to catch it in code since intern 4 uses typescript.


Solution

  • I've spent a bit of time today working on this in the hope of achieving what you're asking.

    I've built my own automation framework both in my workplace and for my own solo project using Intern 4 and didn't realise that this kind of thing had been made so complicated with Intern 4.

    As it stands, I achieved the result you are looking for via the following solution.

    I am running my intern functional suite via a custom npm script like you're describing already. I have added my own script to my package.json file and am sending in two parameters which I have called myArg1 and myArg2 just for this example.

    So my package.json script is as follows:

    "scripts": {
        "test-ui": "./node_modules/.bin/intern config=/path_to_my_intern.json_config myArg1 myArg2"
    }
    

    Now I can execute this by using npm run test-ui.

    So how to retrieve the two parameters in your intern test? You can use the following:

    process.argv[3]; // Will return 'myArg1'
    process.argv[4]; // Will return 'myArg2'
    

    Basically, the use of process.argv will return an array of all arguments and my two additional ones were passed in at index 3 and index 4. You will find your command line arguments somewhere in that array (perhaps at the same index positions as mine).

    Hope this helps!