dig*_*jim 5 ruby-on-rails capybara rspec-rails
水豚没有找到我的复选框的标签,而且我知道我通过它的标签正确引用了它。我做错了什么,还是这是水豚的一个错误?
根据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
Run Code Online (Sandbox Code Playgroud)
以下是测试结果的要点,包括为页面生成的 html: https: //gist.github.com/anonymous/11270055
根据测试结果,水豚找不到标有“选项1”的复选框,而它显然在生成的html中。
作为旁注,我还注意到,只有当我让 Rails FormHelper 显示字段的默认标签文本时, 我才能通过 Capybara 的标签填写表单。
例如:名为“email_address”的字段的 FormHelper 标签文本显示为“电子邮件地址”,并且fill_in "Email address", with: "blahblah@blah.com"有效。如果我不使用 FormHelper 生成标签,而是将标签设为“电子邮件”,则fill_in "Email", with: "blahblah@blah.com"不起作用,因为水豚找不到标有“电子邮件”的字段。看来这种行为与所有表单元素一致(或者至少与文本字段和复选框一致——这是我迄今为止测试过的唯一的)。
选项 1 的 html 如下所示:
<label class="control-label" for="__Option:0x000001061be660_">Option 1</label>
<div class="controls">
<input id="options_" name="options[]" type="checkbox" value="1" />
</div>
Run Code Online (Sandbox Code Playgroud)
问题是标签和复选框没有正确关联:
for属性为“__Option:0x000001061be660_”id属性“options_”结果,Capybara 找到了标签“Option 1”,但没有找到与复选框关联的标签。
需要更新页面以使标签for属性和复选框id属性匹配。例如:
<label class="control-label" for="__Option:0x000001061be660_">Option 1</label>
<div class="controls">
<input id="__Option:0x000001061be660_" name="options[]" type="checkbox" value="1" />
</div>
Run Code Online (Sandbox Code Playgroud)