spyoungtech / behave-webdriver

Selenium webdriver step library for use with the behave BDD testing framework
MIT License
62 stars 21 forks source link

wildcards in step definitions? #72

Closed mikelietz closed 5 years ago

mikelietz commented 5 years ago

I don't think this is a bug as much as lack of coding experience. I'm porting over some very rudimentary lettuce_webdriver features and steps, and I'm struggling for a clean replacement for "I should see..."

One such feature line for this is:

Scenario: Testing Webdriver
  When I open the url "http://my.login.page"
  Then I should see "Forgot Your Password?"

Of the three definitions below, only the first one works. The latter two are not recognized and behave suggests a snippet like the first one.

@then(u'I should see "Forgot Your Password?"')
def step_impl(context):
  assert context.behave_driver.element_contains('body', 'Forgot Your Password?')

@then(u'I should see "([^"]*)?"')
def step_impl(context, value):
  assert context.behave_driver.element_contains('body', value)

@then(u'I should see "([^"]*)?"')
def step_impl(context, value):
  context.execute_steps( u"""
    then I expect that element "body" contains the text "{text}')
  """).format(text = value)
spyoungtech commented 5 years ago

By default, the 'parse' matcher is in use.
Defining a generic step for this use should be as simple as the following:

@then(u'I should see "{text}"')
def expect_some_text(context, text):
    assert context.behave_driver.element_contains('body', text)

The problem you're encountering with the latter two steps is that, in order to use regex matching, you'd need to change the step matcher first.

So you could define the same kind of step using the regex matcher this way

from behave import use_step_matcher
use_step_matcher('re')

@then(u'I should see "([^"]*)?"')
def step_impl(context, value):
  assert context.behave_driver.element_contains('body', value)

If you change the matcher, don't forget to change it back as needed, so your other steps don't unexpectedly stop working.

For more info, you can read about this more in the behave documentation

mikelietz commented 5 years ago

Thank you! I wasn't sure if this was better asked there or here.