【发布时间】:2010-11-13 12:39:26
【问题描述】:
我将 webrat 与黄瓜一起使用,我想测试当我在页面上时是否已选中单选按钮。 我怎样才能做到这一点 ?我没有在 webrat 中找到任何可以做到这一点的步骤。
【问题讨论】:
标签: radio-button cucumber webrat checked
我将 webrat 与黄瓜一起使用,我想测试当我在页面上时是否已选中单选按钮。 我怎样才能做到这一点 ?我没有在 webrat 中找到任何可以做到这一点的步骤。
【问题讨论】:
标签: radio-button cucumber webrat checked
expect(find_field("radio_button_name")).to be_checked
【讨论】:
input("#my_box").should be_checked
【讨论】:
find_field 方法成功了。
在某些情况下,您不能依赖具有 id 或标签或标签文本发生变化的复选框。在这种情况下,您可以使用 webrat 中的 have_selector 方法。
来自我的工作代码(我的复选框上没有 id)。
response_body.should have_selector 'input[type=radio][checked=checked][value=information]'
说明:如果文档正文包含已选中且值为“信息”的单选按钮 (input[type=radio]),则测试将返回 true
【讨论】:
刚刚将 web_step 复选框更改为单选按钮
将以下步骤添加到 web_steps.rb
Then /^the "([^"]*)" radio_button(?: within "([^"]*)")? should be checked$/ do |label, selector|
with_scope(selector) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_true
else
assert field_checked
end
end
end
您可以编写以下代码来检查给定的raido按钮是否被选中
And the "Bacon" radio_button within "div.radio_container" should be checked
【讨论】:
您可以使用 web_steps.rb 中的内置复选框匹配器:
And the "Bacon" checkbox should be checked
但是,您需要在复选框上有一个与相应复选框输入字段的 ID 相匹配的标签。 Rails 中的 f.label 助手接受一个字符串作为第一个参数中的 ID。您可能必须构建一个包含字段名称和复选框名称的字符串:
f.label "lunch_#{food_name}, food_name
f.radio_button :lunch, food_name
在任何情况下,使用此指令来查看您的 HTML 是否正确:
Then show me the page
【讨论】:
包装了 Jesper Rønn-Jensen 他的函数 + 添加了 rails 使用的名称:
Then /^I should see that "([^"]*)" is checked from "([^"]*)"$/ do |value, name|
page.should have_selector "input[type='radio'][checked='checked'][value='#{value}'][name='#{name}']"
end
【讨论】:
And the "Obvious choice" checkbox should be checked
虽然它可能是一个单选按钮,但代码会起作用。它只是检查标有该文本的字段。
【讨论】:
你可以在你的领域使用checked?方法
expect(find_field("radio_button_id").checked?).to eq(true)
【讨论】: