Dom*_*ann 44
这样做:
find("#id_of_hidden_input", :visible => false).value
Run Code Online (Sandbox Code Playgroud)
gla*_*asz 10
匹配器has_field?也适用于隐藏字段。没有必要在这种情况下find或在这种情况下做奇怪的体操all。
page.has_field? "label of the field", type: :hidden, with: "field value"
page.has_field? "id_of_the_field", type: :hidden, with: "field value"
Run Code Online (Sandbox Code Playgroud)
这里的关键是显式地设置:type选项:hidden。
为什么要使用带有隐藏字段的标签?如果您使用像 flatpickr 这样的 js 库,它会掩盖您的原始文本字段以隐藏它,这会派上用场。不将行为测试与特定标记耦合总是一件好事。
您还可以指示 Capybara 不要在您的spec_helper.rb或等效项中全局忽略隐藏元素。可以覆盖默认行为:
# default behavior for hidden elements
# Capybara.ignore_hidden_elements = false
# find all elements (hidden or visible)
page.all(".articles .article[id='foo']")
# find visible elements only (overwrite the standard behavior just for this query)
page.all(".articles .article[id='foo']", :visible => true)
# changing the default behavior (e.g. in your features/support/env.rb file)
Capybara.ignore_hidden_elements = true
# now the query just finds visible nodes by default
page.all(".articles .article[id='foo']")
# but you can change the default behaviour by passing the :visible option again
page.all(".articles .article[id='foo']", :visible => false)
Run Code Online (Sandbox Code Playgroud)
取自这篇文章的例子。
find_by_css它很简单,您可以通过使用或使用 xpath 来完成,如下所示
page.find_by_css('#foo .bar a') #foo is the id the foo has .bar class
page.find('/table/tbody/tr[3]') #path of the element we want to find
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11492 次 |
| 最近记录: |