radish-bdd / radish

Behavior Driven Development tooling for Python. The root from red to green.
https://radish-bdd.github.io
MIT License
182 stars 49 forks source link

How to pass simple Text in feature File with Radish to the Step Function #379

Closed dschiller closed 5 years ago

dschiller commented 5 years ago

The first Scenario doesn't work. The second works. The third Scenario is also not working. I have no Idea how to pass Outline Data to the Steps. With numbers it's so easy. How is it with simple Text not Textblocks ? I studied the Radish Documentation but couldn't find any Solution. Also the Examples for Outlines is extremly rare. Some more Examples would be amazing !!

editabe_combobox.feature:

Feature: Editable Combobox can get any Text

   In the editable Combobox a User is able to
   enter any Text.

   Scenario: Enter Text "Test Item 01"
      Given I have the Text "Test Item 01"
      When I type the Text
      Then I expect the editable Combobox takes "Test Item 01"

   Scenario: Enter Text "Otaw"
      Given I have the Text
         """
         Otaw
         """
      When I type the Text
      Then I expect the editable Combobox takes "Otaw"

   Scenario Outline: Enter Text
      Given I have the Text <text>
      When I type the Text
      Then I expect the editable Combobox takes the <text>

   Examples:
      | text         |
      | Test Item 01 |
      | Test Item 02 |

step.py:

from radish import given, when, then

@given('I have the Text')
def step_impl(step):
    step.context.text = step.text

@when('I type the Text')
def step_impl(step):
    step.context.result = step.context.text

@then('I expect the editable Combobox takes "Otaw"')
def step_impl(step):
    # assert step.context.result == step.text

@then('I expect the editable Combobox takes "Test Saved Item 01"')
def step_impl(step):
    # assert step.context.result == step.text
timofurrer commented 5 years ago

The trick is having the Step text accept an argument (like described here: https://radish.readthedocs.io/en/latest/tutorial.html#step-pattern):

@given('I have the Text {text:QuotedString}')

However, I noticed that you want to use either a text from the Step text or from the Step doc string. For that you need a step pattern a little bit more complicated:

from radish import given, when, then, custom_type

@custom_type("optional_whitespace", r"\s*")
def whitespace_or_not_not(text):
    return text

@given('I have the Text{whitespace:optional_whitespace}{text:QuotedString?}')
def step_impl(step, whitespace, text):
    assert text is not None or step.text is not None, \
        "You have to provide a text in the step text or step doc string"

    step.context.text = text if text is not None else step.text

The entire steps.py file will look like this:

def whitespace_or_not_not(text):
    return text

@given('I have the Text{whitespace:optional_whitespace}{text:QuotedString?}')
def step_impl(step, whitespace, text):
    assert text is not None or step.text is not None, \
        "You have to provide a text in the step text or step doc string"

    step.context.text = text if text is not None else step.text

@when('I type the Text')
def step_impl(step):
    step.context.result = step.context.text

@then('I expect the editable Combobox takes {text:QuotedString}')
def step_impl(step, text):
    assert step.context.result == text

The Feature File also had typos. To make it work:

Feature: Editable Combobox can get any Text

   In the editable Combobox a User is able to
   enter any Text.

   Scenario: Enter Text "Test Item 01"
      Given I have the Text "Test Item 01"
      When I type the Text
      Then I expect the editable Combobox takes "Test Item 01"

   Scenario: Enter Text "Otaw"
      Given I have the Text
         """
         Otaw
         """
      When I type the Text
      Then I expect the editable Combobox takes "Otaw"

   Scenario Outline: Enter Text
      Given I have the Text "<text>"
      When I type the Text
      Then I expect the editable Combobox takes "<text>"

   Examples:
      | text         |
      | Test Item 01 |
      | Test Item 02 |
dschiller commented 5 years ago

Thank's a lot Timo. I didn't understand, that you can use just ':QuotedString'.