views:

746

answers:

3

Cucumber generates out some neat webrat regex steps. I encountered a problem when I tried the this.

In feature:

And I fill in "Telephone (Home)" with "61234567"

In webrat steps:

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value) 
end

The error encountered:

Could not find field: "Telephone (Home)" (Webrat::NotFoundError)

It seems that the parenthesis between "Home" is giving problem. How do I tweak the regex to account for parenthesis?

UPDATE:

It seems that the regex wasn't the problem as the "field" instance variable did yield "Telephone (Home)". The real problem was the way webrat's "fill_in" method parses the field variable.

+1  A: 

If you only want to capture "Telephone" try this:

/^I fill in "(\w+).*?" with "([^\"]*)"$/

If it's "Home" you're after try this:

/^I fill in "(?:.*?\()?(.+?)\)?" with "([^\"]*)"$/;
Inshallah
A: 

This encountered me too with the field "(log out)"...

You could call for the id field?

fill_in("user_telephone_home", :with => data)
Lichtamberg
Yup I ended up using field's id although it sticks out like a sore thumb in my cucumber feature definitions. Thanks!
JasonOng
A: 

I had a similar problem with matching labels to fields in webrat, and I came up with this code snippet which loosens the regexp used to match a label to a field. Maybe it will help you out.

I have this in my features/support/env.rb

module Webrat
  module Locators
    class FieldLabeledLocator < Locator
      def matching_label_elements_with_numbering
        label_elements.select do |label_element|
          text(label_element) =~ /^.*#{Regexp.escape(@value.to_s)}.*$/i
        end
      end
      alias_method_chain :matching_label_elements, :numbering
    end
  end
end

http://gist.github.com/169215

phinze