How do I make a word optional in a Cucumber step definition?
Asked Answered
P

1

9

I have a step definition, below, that does what I want it to do i.e. it checks the url of the page against the 'page' element of 'PAGES' hash.

Then(/^I should( still)? be at the "(.*)" page$/) do |still, page|
  BROWSER.url.should == PAGES[page]
end

The step definition is used for both

  • I should be at the ... page
  • I should still be at the ... page

However, I don't need "still" to be passed into the block. I just need it to be optional for matching to the step but not passed into the block. How can I do that?

Thanks.

Payment answered 24/2, 2017 at 16:39 Comment(0)
H
15

You want to mark the "still" group as non-capturing. This is done by starting the group with ?:.

Then(/^I should(?: still)? be at the "(.*)" page$/) do |page|
  BROWSER.url.should == PAGES[page]
end
Hominid answered 24/2, 2017 at 16:43 Comment(2)
What if I need the "still" to be passed into the block (if it's there), how would I do that?Jutland
@Richard, you would not specify the non-capturing flag, basically it would be back to what was in the original question.Hominid

© 2022 - 2024 — McMap. All rights reserved.