黄瓜步骤中的正则表达式

发布于 2024-07-29 03:04:22 字数 656 浏览 4 评论 0原文

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

在功能中:

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

在 webrat 步骤中:

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

遇到的错误:

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

似乎“Home”之间的括号出现了问题。 如何调整正则表达式以考虑括号?

更新:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错爱 2024-08-05 03:04:22

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

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

如果您要捕获“家庭”,请尝试以下操作:

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

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 "([^\"]*)"$/;
初熏 2024-08-05 03:04:22

我也遇到了“(注销)”字段...

您可以调用 id 字段吗?

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

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

You could call for the id field?

fill_in("user_telephone_home", :with => data)
玩套路吗 2024-08-05 03:04:22

我在将标签与 webrat 中的字段匹配时遇到了类似的问题,我想出了这个代码片段,它放松了用于将标签与字段匹配的正则表达式。 也许它会对你有所帮助。

我的 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文