Pete Bonney
Pete Bonney

Reputation: 3

Using regex in Ruby/Capybara case statement throws 'Regex matched 0 arguments' error

I'm testing a site using Cucumber/Selenium Web-driver & Capybara I'm trying to build a case statment in my step_definition so that I don't have multiple "given" for each page on my site.

I've got the following:

Given /^I am on the .+ page$/ do |page_name|
  case page_name
  when "I am on the home page"
   visit ('/')
  else
    puts "page #{page_name} not found"
  end 
end

When i run cucumber in the terminal to run the tests i get

Given I am on the home page # features/step_definitions/home_page.rb:3 Your block takes 1 argument, but the Regexp matched 0 arguments. (Cucumber::Glue::ArityMismatchError) features/step_definitions/home_page.rb:3:in /^I am on the .+ page$/' features/homepage.feature:5:inGiven I am on the home page'

I've tried wrapping the regex in (), {}, [], " ", '' and various combinations of these, I've checked the indentation of my code and i think it's correct.

I've put my Regex in to an online validator and it picks it up fine.

Upvotes: 0

Views: 133

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

Your block is taking a parameter page_name but your Regex doesn't have a capture group in it to be used as that parameter. You probably want something like

Given /^I am on the (.+) page$/ do |page_name|

so the .+ portion is captured and passed through as page_name

Upvotes: 1

Related Questions