0

I am using Cucumber java. In a step definition, I would like to use a question mark as a string and not to be considered as a regex, which means I need to escape it. I have seen this topic about escaping and tried to apply :

@When("^I make PUT /service/(.*)/url\\?pathParam=(.*)$")

In the feature file, the step would appear as :

When I make PUT /service/<string>/url\?pathParam=<string>

(please note the backslash in front of the question mark)

I would like to get :

When I make PUT /service/<string>/url?pathParam=<string>

How should I write my step so the question mark is correctly escaped, with no additional character added?

Edit : Using a single backslash in front of the question mark results with an illegal escape character in string literal exception. The curious thing is that escaping a quote with a single backslash anywhere in the step doesn't display this exception.

Community
  • 1
  • 1
M Bellou
  • 1
  • 1
  • 3
  • Why then use ``\\`` at all? Write it as `@When("^I make PUT /service/(.*)/url?pathParam=(.*)$")` However, I feel you really need to escpae the `?` as `\?` to match a literal `?` with a regex. – Wiktor Stribiżew May 19 '16 at 10:59
  • not escaping the question mark makes it recognized as a regex, and then, it disappears on the step, giving : When I make PUT /service/pathParam= – M Bellou May 19 '16 at 12:05
  • Ok, try a usual workaround: `@When("^I make PUT /service/(.*)/url[?]pathParam=(.*)$")` - a character class. – Wiktor Stribiżew May 19 '16 at 12:14
  • It doesn't work, result is : When I make PUT /service/[]pathParam= – M Bellou May 19 '16 at 12:20
  • Is `?` a special character in your environment? How should be written so that `?` was a literal? – Wiktor Stribiżew May 19 '16 at 12:22
  • It happens for any Meta-character, for example, I have the same issue with $. For non meta-character, escaping works fine. From the solution mentionned in my question, it seems necessary to use slashes, but I already use it to define the url in my step definition. – M Bellou May 19 '16 at 12:32
  • The stepdef you tried works perfectly for me with [cucumber-jvm](https://cucumber.io/docs/reference/jvm#java), that is the step is executed and the strings are passed to the glue. I guess your question is not about escaping in Cucumber Java, but rather about making your IDE integration to show autocompletion without escaped characters when you typing a feature? – Mykola Gurov May 20 '16 at 06:02
  • Thanks Mykola, indeed, this is what I am figuring out too with different answers I am getting. Can you please tell me which version of cucumber-jvm you use and which IDE you use (+plugins)? – M Bellou May 20 '16 at 08:27
  • I checked [the latest revision of skeleton project](https://github.com/cucumber/cucumber-java-skeleton/tree/e1a7dbb74379fa51ce0590aafcd1c935e27f5a8f) which uses cucumber 1.2.4. Would work with much earlier versions as well - java regex doesn't change often. I use IntelliJ with Cucumber for java 143.382 where the gherkin autocomplete *does not* work properly. But this is an old issue of the Idea plugin rather than cucumber, IMHO. – Mykola Gurov May 20 '16 at 16:55

1 Answers1

-1

This should work:

if you're trying to achieve this When I make PUT /service/<string>/url?pathParam=<string>:

@When("^I make PUT /service/\"(.*?)\"/url\\?pathParam=\"(.*?)\"$")

useful info here related to this: How to escape special characters in Cucumber step definitions?

Community
  • 1
  • 1
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
  • The link you provide is the one I mention in my initial question. Your solution gives : When I make PUT /service/""\?pathParam="" . It displays quotes and a backslash that I don't want to have. – M Bellou May 19 '16 at 12:08
  • I edited the initial question to mention that escaping quotes works fine, but escaping question mark doesn't. Trying o escape $ anywhere in the step definition results with \$, not like in the link you provide. It seems to be related to slashes added at the beginning and end of step definition. Issue in my case is that I am already using slashes to define the url I am using in the step definition. From the link solution, what is the use of the slashes? – M Bellou May 19 '16 at 12:16
  • updated my answer.. pls check if i understood correctly – Ranjith's May 19 '16 at 12:31
  • No, it doesn't work. It gives : When I make PUT /service/".*"/url\?pathParam=".*" – M Bellou May 19 '16 at 12:35
  • there is no url mentioned in my step.. this will give you `I make PUT /service/"test"?pathParam="test"` – Ranjith's May 19 '16 at 12:37
  • Your latest edit displays : When I make PUT /service/".*"/url\?pathParam=".*" – M Bellou May 19 '16 at 12:55
  • try copy and paste it shouldn't give `url\?path`.. working for me – Ranjith's May 19 '16 at 12:58
  • Copy paste provides same results than my previous comment. Could it be related to cucumber dependencies or IDE? I am using IntelliJ with plugin Gherkin v141.178, cucumber for java v141.178. I use following dependencies : cucumber-spring, cucumber-java, cucumber-jvm, cucumber-core all from info.cukes v1.2.4 – M Bellou May 19 '16 at 16:55
  • Suggested step definition is wrong as it requires extra quotes from url. – Mykola Gurov May 20 '16 at 06:02