Search code examples
javascriptregexcucumbergherkincucumberjs

retrieving a parameter ending before a specific string in cucumber-js


I tried to set this regex for an authentication Given step with two optional parameters:

Given(/^I log in as ([A-Za-z\-]+)(?: with the contract ((?:.(?!for the protal))+))?(?: for the protal (.*))?$/, 
  function(user, contract, portal, callback) { 
    console.log({user, contract, portal}); 
  }
);

Cucumber-js fails to detect the second optional parameter even if it successfully matches the expressions to the Given sentence with one, two or three parameters. I put .(?!for the protal) in the second parameter to end it when the third one (portal) eventually begins as the list of contracts isn't exhaustive.

Here's the output I get for the three cases:

Given I log in as tester1

user: tester1,

contract: undefined,

portal: undefined

OK

Given I log in as tester1 with the contract contract1

user: tester1,

contract: undefined,

portal: undefined

KO

Given I log in as tester1 with the contract contract1 for the protal portal1

user: tester1,

contract: portal1,

portal: undefined

KO

I think there is an issue with detecting this part of the regex .(?!for the protal) in cucumber-js as exactly the same expression works in Java. In addition to that, Cucumber in Java successfully sets to second parameter as undefined when only the first and the third are mentioned (but this is another issue).

Thanks !


Solution

  • There is a bug in cucumber-js abour this issue: https://github.com/cucumber/cucumber-js/issues/1096

    I resolved it temporarily by wrapping the contract by quotes using this expression:

    /^I log in as ([\w-]+)(?: with the contract "([^"]+)")?(?: for the portal (.+))?$/