Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 829
Karl Cummins Avatar asked Feb 24 '17 16:02

Karl Cummins


People also ask

How do you do multiple step definitions on cucumbers?

You can have all of your step definitions in one file, or in multiple files. When you start with your project, all your step definitions will probably be in one file. As your project grows, you should split your step definitions into meaningful groups in different files.

How do you write a code in step definition Cucumber?

Add a Step Definition file 1) Create a new Class file in the 'stepDefinition' package and name it as 'Test_Steps', by right click on the Package and select New > Class. Do not check the option for 'public static void main' and click on Finish button. Take a look at the message in the console window.


1 Answers

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
like image 172
Justin Ko Avatar answered Nov 06 '22 14:11

Justin Ko