Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 分步骤的正则表达式_Ruby On Rails_Ruby_Regex_Cucumber_Webrat - Fatal编程技术网

Ruby on rails 分步骤的正则表达式

Ruby on rails 分步骤的正则表达式,ruby-on-rails,ruby,regex,cucumber,webrat,Ruby On Rails,Ruby,Regex,Cucumber,Webrat,Cucumber生成了一些整洁的webrat正则表达式步骤。我在尝试这个时遇到了一个问题 在功能中: And I fill in "Telephone (Home)" with "61234567" When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value| fill_in(field, :with => value) end Could not find field: "Telephone (Home)" (W

Cucumber生成了一些整洁的webrat正则表达式步骤。我在尝试这个时遇到了一个问题

在功能中:

And I fill in "Telephone (Home)" with "61234567"
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value) 
end
Could not find field: "Telephone (Home)" (Webrat::NotFoundError)
在webrat步骤中:

And I fill in "Telephone (Home)" with "61234567"
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value) 
end
Could not find field: "Telephone (Home)" (Webrat::NotFoundError)
遇到的错误:

And I fill in "Telephone (Home)" with "61234567"
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value) 
end
Could not find field: "Telephone (Home)" (Webrat::NotFoundError)
似乎“家”之间的括号是给予问题。如何调整正则表达式以考虑括号

更新:

And I fill in "Telephone (Home)" with "61234567"
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value) 
end
Could not find field: "Telephone (Home)" (Webrat::NotFoundError)

似乎正则表达式不是问题所在,因为“字段”实例变量确实产生了“Telephone(Home)”。真正的问题是webrat的“fill_in”方法解析字段变量的方式。

如果您只想捕获“电话”,请尝试以下方法:

/^I fill in "(\w+).*?" with "([^\"]*)"$/
/^I fill in "(?:.*?\()?(.+?)\)?" with "([^\"]*)"$/;
如果你想要的是“家”,试试这个:

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

这在字段“(注销)”中也遇到了我

你可以打电话找id字段吗

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

在webrat中将标签与字段匹配时,我遇到了类似的问题,我提出了这个代码片段,它释放了用于将标签与字段匹配的regexp。也许它会帮你

我的
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
模块Webrat
模块定位器
类字段标签定位器<定位器
def匹配_标签_元素_与_编号
标记|元素。选择do |标记|元素|
text(label_元素)=~/^.*{Regexp.escape(@value.to_s)}.*$/i
结束
结束
别名\u方法\u链:匹配\u标签\u元素,:编号
结束
结束
结束

是的,我最终使用了字段的id,尽管在我的cucumber功能定义中,它像一个酸痛的拇指一样突出。谢谢