1

I am trying to implement a BDD step which can be use if the step is referring to singular or plural ex: Then I should see the name "John" is displayed but also I want to use the same step if I have more then one name Then I should see the names "John, George" are displayed

In java you can do this when you implement the step like this: @Step("Then I should see the name? (regex) (:?is|are) displayed")

?- is for the plural and(:? | ) is when you want to replace a word

In feature file when you type (names or name; is or are) it points to the same step

Is there a way to do this in squish?

  • You can use regular expressions in step names, as described at [Using Step Patterns with Regular Expressions](https://doc.froglogic.com/squish/latest/api.bdt.functions.html#api.bdt.functions.regexpplaceholders). Are you certain that this is a relevant problem for your users? What problem do you see with 'Then the name(s) "..." should be displayed'? – frog.ca Nov 28 '18 at 19:51
  • This is relevant because in this way you can reuse a step more often, and also is important to write correct from grammatically point of view. If you can write a step which can be use in more then one situation I think you gain a lot of flexibility. In the book "The Cucumber Book" (https://pragprog.com/book/hwcuc/the-cucumber-book) is described how to better write the steps. – Horatiu Moldovan Nov 29 '18 at 11:23
  • Yes, but what about the example that I provided? It tackles the problem without much of a compromise, while at the same time keeping it simple. – frog.ca Nov 30 '18 at 19:30
  • The example with regex expression can be use as workaround of the problem. In general when you use a regex you want to pass something as a variable and to be use in the method, but I am talking about writing steps in order to be grammatically correct and not use regex which normally is used for passing variables, please see this posts https://stackoverflow.com/questions/43528064/using-proper-grammar-in-gherkin – Horatiu Moldovan Dec 03 '18 at 11:46
  • My example was not about regular expressions. Here is my example again: 'Then the name(s) "..." should be displayed' – frog.ca Dec 03 '18 at 14:24
  • The power of this feature is that you can write just one implemetation for this steps: "Then The results are ..." and "Then the result is..." and in feature file you don't have to write "Then the result(s) is(are)" which is not a clean way to write steps. For more examples you can look at this link: https://agileforall.com/just-enough-regular-expressions-for-cucumber/ on row: I might write something like this: [When(@"^(I'm logged|I log) in as an? (.*)$")] public void LogInAs(string role) { // log in as the given role }; – Horatiu Moldovan Dec 04 '18 at 08:37
  • For the example provided from the link above in feature file you write "When I'm logged in as an ..." or "When I log in as an..." and this is a clean way to write tests. – Horatiu Moldovan Dec 04 '18 at 08:42

1 Answers1

1

See Using Step Patterns with Regular Expressions in the froglogic Squish manual for using regular expressions in BDD steps.

Based on that, the following works for me:

# Use (?:...) because it is non-capturing
# Also see https://docs.python.org/2/library/re.html
@Then("I should see the (?:name|names) \"(.*)\" (?:is|are) displayed", regexp=True)
def step(context, nameOrNamesCommaSeparated):
    """Examples:
        Then I should see the name "John" is displayed
        Then I should see the names "John, George" are displayed
    """

    names = []
    if "," in nameOrNamesCommaSeparated:
        names = nameOrNamesCommaSeparated.split(",")
        for i, n in enumerate(names):
            names[i] = n.strip()
    else:
        names = [nameOrNamesCommaSeparated]

    for i, n in enumerate(names):
        test.log("Name #%s: %s" % (i, n))
frog.ca
  • 684
  • 4
  • 8