Capybara 没有找到我的复选框的标签,我知道我通过它的标签正确引用了它。我做错了什么,或者这是 capybara 的一个错误?
根据 http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions:check , "可以通过名称、ID 或标签文本找到复选框。"
这是我运行的请求规范部分:
describe "with valid information" do
it_should_behave_like "all item pages"
before { valid_create_item }
it "should create an item" do
expect { click_button submit }.to change(Item, :count).by(1)
end
describe "after saving the item" do
before { click_button submit }
it { should have_link('Sign out') }
it { should have_selector('h1', text: "Items") }
it { should have_title("Items") }
it { should have_success_message }
end
describe "and options selected" do
before do
puts page.html
check('Option 1')
click_button "Save changes"
end
it { should have_title("Items") }
it { should have_success_message }
it { should have_link('Sign out', href: signout_path) }
specify { expect(item.reload.options.find_by_name("Option 1")).to eq true }
end
这是结果测试的要点,包括为页面生成的 html:https://gist.github.com/anonymous/11270055
根据测试结果,Capybara 在生成的 html 中显然找不到标记为“选项 1”的复选框。
作为旁注,我还注意到只有当我让 Rails FormHelper 显示字段的默认标签文本时,我才能使用 Capybara 的标签填写表单。
例如:名为“email_address”的字段的 FormHelper 标签文本显示为“电子邮件地址”,而
fill_in "Email address", with: "blahblah@blah.com"
作品。如果我不使用 FormHelper 生成标签,而是将标签设为“电子邮件”,fill_in "Email", with: "blahblah@blah.com"
不起作用,因为 Capybara 找不到标有“电子邮件”的字段。似乎这种行为与所有表单元素(或至少与文本字段和复选框 - 迄今为止我测试过的唯一元素)一致。
最佳答案
您还可以使用 check(\[locator\], options)
method 让 Capybara 找到复选框。通过将复选框的值作为选项传递。
代码是page.check(name, option: value)
,其中 name 是复选框的名称和 value - 它的值。
这也适用于 id
而不是 name
.
关于ruby-on-rails - 如何让 Capybara 通过标签检查复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280642/