Ruby-on-rails fill_in 在 Rspec/Capybara 中如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26072095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:56:05  来源:igfitidea点击:

How does fill_in work in Rspec/Capybara?

ruby-on-railsrspeccapybara

提问by Joey Hu

I am following Michael Hartl's Ruby on Rails Tutorial. When I use rspec/capybara, the fill_in method makes me confused. I have the following view code:

我正在关注 Michael Hartl 的 Ruby on Rails 教程。当我使用 rspec/capybara 时,fill_in 方法让我感到困惑。我有以下视图代码:

 <%= f.label :name %>   
 <%= f.text_field :name %>

This is my testing code:

这是我的测试代码:

 fill_in "Name", with: "Example User"

It seems that label and text_field are both required for fill_in to locate the input field. If I either take off f.labelor change <%= f.text_field :name %>to be <%= f.text_field :another_name %>, the test will give me ElementNotFounderror. Can anyone explain how the fill_in works here? Are input field and label both required for fill_inmethod?

fill_in 似乎都需要 label 和 text_field 来定位输入字段。如果我起飞f.label或更改 <%= f.text_field :name %><%= f.text_field :another_name %>,则测试会给我ElementNotFound错误。谁能解释一下 fill_in 在这里是如何工作的?方法是否需要输入字段和标签fill_in

回答by twonegatives

It is statedthat fill_inlooks for field name, idor label text. According to ActionView::Helpers::FormHelpersection of rails guides, the view code which you ask about should be translated to the following html code:

的是fill_in长相现场nameid或标签文本。根据Rails 指南的ActionView::Helpers::FormHelper部分,您询问的视图代码应转换为以下 html 代码:

# I assume that you made a form for a @user object
<label for="user_name">
  Name
</label>
<input id="user_name" name="user[name]" type="text" />

As you see, labelproduced the "Name" text, which you ask for inside of your fill_inexpression. But idand nameproperties of inputfield are slightly different, so you should have been using idbased selector to achieve the same result:

如您所见,label生成了您在fill_in表达式中要求的“名称”文本。但是id和字段的name属性input略有不同,因此您应该一直使用id基于选择器来实现相同的结果:

fill_in "user_name", with: 'Example User'

fill_in "user_name", with: 'Example User'

So, to sum up, labelfield is not required, but you should watch for your html code carefully and select the appropriate parameters for fill_inexpression.

所以,总而言之,labelfield不是必需的,但你应该仔细观察你的html代码并选择合适的fill_in表达式参数。

回答by 7stud

Just to add to what twonegatives posted. Here's what the capybara docs say for the fill_in() method:

只是为了添加到 twonegatives 发布的内容。以下是水豚文档对 fill_in() 方法的说明:

The field can be found via its name [attribute], id [attribute,] or label text

可以通过名称 [attribute]、id [attribute,] 或标签文本找到该字段

http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Actions:fill_in

http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Actions:fill_in

When I delete the 'Name' label, I can use any of the following and the tests still pass:

当我删除“名称”标签时,我可以使用以下任何一种并且测试仍然通过:

 fill_in 'user_name',   with: "Example User"  #locate text field by id attribute
 fill_in :user_name,    with: "Example User"  #locate text field by id attribute

 fill_in 'user[name]',  with: "Example User"  #locate text field by name attribute
 fill_in :'user[name]'  with: "Example User"  #locate text field by name attribute

In ruby, some characters cannot be used in a symbol name unless the whole symbol is quoted.

在 ruby​​ 中,除非整个符号被引用,否则某些字符不能在符号名称中使用。

Capybara must be retrieving all the text fields (or text areas) from the page, then getting the values of the id and name attributes(easily done with something like Nokogiri) then checking if either value is equal to the first argument to fill_in() (after converting the first argument to a String via to_s()).

Capybara 必须从页面中检索所有文本字段(或文本区域),然后获取 id 和 name 属性的值(使用 Nokogiri 之类的东西很容易完成),然后检查其中一个值是否等于 fill_in() 的第一个参数(在通过 to_s() 将第一个参数转换为字符串之后)。

回答by urubuz

I provided my 2 cents here: Capybara not finding form elements.

我在这里提供了我的 2 美分:Capybara not find form elements

If you call the Rails text_field helper with underscore (:first_name), the DOM gets rendered as "First name" and its what Capybara will need. No need for an id attribute.

如果您使用下划线 (:first_name) 调用 Rails text_field 助手,DOM 将呈现为“名字”以及 Capybara 需要的内容。不需要 id 属性。

View: <%= form.text_field :first_name %>

看法: <%= form.text_field :first_name %>

Test: fill_in "First name", with: 'Elaine'

测试: fill_in "First name", with: 'Elaine'