与Capybara一起寻找禁用的场地

Pas*_*auf 18 ruby-on-rails capybara

我有一个带有标签的选择框:

<label for="slide_orientation">Slide orientation</label>
<select disabled="" class="property" id="slide_orientation" name="slide_orientation">
  <option value="horizontal">Horizontal</option>
  <option value="vertical" selected="selected">Vertical</option>
</select>
Run Code Online (Sandbox Code Playgroud)

如您所见,禁用了选择框.当我尝试找到它时field_labeled("Slide orientation"),它会返回一个错误:

Capybara::ElementNotFound: Unable to find field "Slide orientation"
from /Users/pascal/.rvm/gems/ruby-1.9.3-p392/gems/capybara-2.0.2/lib/capybara/result.rb:22:in `find!'
Run Code Online (Sandbox Code Playgroud)

如果未禁用选择框,则field_labeled("Slide orientation")返回select元素就好了.

这是预期的行为吗?如果是这样,我将如何寻找残疾元素?在我的情况下,我需要它来测试它是否被禁用.

Shu*_*awa 33

Capybara 2.1.0支持disabled过滤.您可以使用它轻松找到禁用的字段.

field_labeled("Slide orientation", disabled: true)
Run Code Online (Sandbox Code Playgroud)

您需要明确指定它,因为disabled默认情况下过滤器处于关闭状态.


And*_*tad 10

如果它具有disabled属性,则通过此方法.

js: true和运行page.evaluate_script.

it "check slider orientation", js: true do
    disabled = page.evaluate_script("$('#slide_orientation').attr('disabled');")
    disabled.should == 'disabled' 
end
Run Code Online (Sandbox Code Playgroud)

更新

或者你可以使用 have_css

page.should have_css("#slide_orientation[disabled]") 
Run Code Online (Sandbox Code Playgroud)

(被盗形式这个优秀的答案)

  • `expect(find("slide_orientation")).to be_disabled` (2认同)

rma*_*ero 5

由于这个问题的答案很旧并且从那时起事情已经发生了一些变化,这是一个UPDATE

如果您只想检查某个字段是否被禁用,您现在可以执行以下操作:

expect(page).to have_field 'Slide orientation', disabled: true
Run Code Online (Sandbox Code Playgroud)

根据这个公关:https : //github.com/teamcapybara/capybara/issues/982


Pas*_*auf 3

安德烈亚斯和这个答案让我走上了最终解决方案的轨道。可以通过以下方式找到具有特定标签(而不是 HTML id)的禁用字段:

label_field = all("label").detect { |l| (l.text =~ /#{label}/i).present? }
raise Exception.new("Couldn't find field '#{label}'") if label_field.nil?
the_actual_field = first("##{label_field[:for]}")
Run Code Online (Sandbox Code Playgroud)

检查该字段是否被禁用可以使用一条语句来完成:

page.should have_css("##{label_field[:for]}[disabled]") 
Run Code Online (Sandbox Code Playgroud)

它仍然感觉像是一种解决方法,而不是最好的水豚式解决方案,但它确实有效!