Is there a way in squish to use plural and singular in same BDD step?

3.4k Views Asked by At

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?

1

There are 1 best solutions below

4
On

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))