2

I'd want to insert a predefined string into my step definition in Cucumber using java. Here's what I have:

Java Code

public String temp = "hello";
//insert temp into my step:
@Then("^the "+temp+" should be empty$")
public void the_should_be_empty() throws Throwable {
    //do things
}

But I keep getting this error:

"The value for annotation attribute Then.value must be a constant expression"

So, how do I insert a string into my capturing step?

=============

More Info

I am trying to have a set list of "global keywords" used in many of my BDD step definitios. So when I add a new "global keyword", it will be changed in all my BDD. For example (red|yellow|green) could be used in 10 different BDD steps, and I want to add blue without changing all 10 steps. Instead I want a String variable that contains the list, then insert this variable into my BDD.

Katie
  • 45,622
  • 19
  • 93
  • 125
  • try to add `final` key word it makes every thing constant – Neelay Srivastava Nov 04 '16 at 18:30
  • @NeelaySrivastava good idea! But it didn't work :( – Katie Nov 04 '16 at 19:04
  • Can you pass the string in the feature file and use a non-capturing group in your step definition - (?:your text). – Grasshopper Nov 04 '16 at 19:13
  • BTW what are you trying to accomplish here? – Grasshopper Nov 04 '16 at 19:13
  • AFAIR should be static final to work. Cannot be variable. This is cucumber but java annotations specific. – Mykola Gurov Nov 05 '16 at 17:40
  • 2
    what is the purpose? if you want to allow synonyms [QAF](https://qmetry.github.io/qaf) supports that for example `user (login|signin) with {username} and {password}`. This can be used as `user login with "user" and "passcode"` or `user signin with "user" and "passcode"` You also can run your [gherkin features files with QAF](https://qmetry.github.io/qaf/latest/gherkin_client.html) – user861594 Nov 08 '16 at 06:02
  • Just updated the question with what I am trying to accomplish – Katie Nov 08 '16 at 21:42

2 Answers2

5

The short answer is: "You don't".

The longer is that the value in the annotation must be a constant. It cant be something that is constructed run time.

The way Cucumber matches steps between Java and scenarios is by using the regular expression you define in the annotation. The process fails if the value is constructed run time. The Cucumber runner will locate and use all the regular expressions found in the step implementations and then search the feature files to match code with scenario steps.

This is why you can't build the string to match against run time.

It would be interesting to understand why you want to build the string run time. What are you trying to achieve? A consequence of creating many different strings is that there must be many different steps in your scenarios that should match. To me, it feels like you have misunderstood something. Please share what you are trying to achieve and maybe we can help you with another approach.

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25
2

You can achieve this by using a custom parameter type.

Once defined, your step def will look like this:

@Then("the {color} should be empty")
public void the_should_be_empty(Color color) throws Throwable {
    //do things
}

Now if the list of colors changes, you don't have to edit every stepdef.

Seb Rose
  • 3,628
  • 18
  • 29