简体   繁体   中英

Regular expressions in cucumber steps

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.

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 "([^\"]*)"$/;

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

You could call for the id field?

fill_in("user_telephone_home", :with => data)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM