如何确定单选按钮是否已被选中?

发布于 2024-07-27 22:01:07 字数 92 浏览 6 评论 0原文

我正在使用带有黄瓜的 webrat,我想测试当我在页面上时是否已经选中了单选按钮。 我怎样才能做到这一点 ? 我在 webrat 中没有找到任何可以做到这一点的步骤。

I'm using webrat with cucumber and I would like to test if a radio button is checked already when I am on a page.
How can I do that ? I didn't find any step in webrat which can do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

云归处 2024-08-03 22:01:18

您可以在您的字段上使用方法 checked?

expect(find_field("radio_button_id").checked?).to eq(true)

You can use method checked? on your field

expect(find_field("radio_button_id").checked?).to eq(true)
梦幻的味道 2024-08-03 22:01:16
    And the "Obvious choice" checkbox should be checked

虽然它可能是一个单选按钮,但代码可以工作。 它只是检查标有该文本的字段。

    And the "Obvious choice" checkbox should be checked

Although it might be a radio button, but the code will work. It is just checking for a fields labelled with that text.

心不设防 2024-08-03 22:01:15

包装了 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

Wrapped Jesper Rønn-Jensen his function + added name which is used by 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
寄人书 2024-08-03 22:01:14

您可以使用 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

You can use the built-in checkbox matcher in web_steps.rb:

And the "Bacon" checkbox should be checked

However, you'll need to have a label on your checkbox that matches the ID of the corresponding checkbox input field. The f.label helper in Rails takes a string to use as the ID in the first argument. You may have to build a string that includes the field name and the checkbox name:

f.label "lunch_#{food_name}, food_name
f.radio_button :lunch, food_name

In any case, use this directive to see that you've got the HTML correct:

Then show me the page
北凤男飞 2024-08-03 22:01:13

只需将 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

Just changed a web_step checkbox to radio button

Add the following step to 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

And you can write the following to check whether the given raido button is checked or not

And the "Bacon" radio_button within "div.radio_container" should be checked
℉服软 2024-08-03 22:01:12

在某些情况下,您不能依赖具有 id 或标签的复选框,或者标签文本会发生变化。 在这种情况下,您可以使用 webrat 中的 have_selector 方法。

从我的工作代码(我的复选框上没有 id)。

response_body.should have_selector 'input[type=radio][checked=checked][value=information]'

说明:如果文档正文包含已选中且值为“information”的单选按钮 (input[type=radio]),则测试将返回 true

There are cases when you cannot rely on checkboxes having ids or labels or where label text changes. In this case you can use the have_selector method from webrat.

From my working code (where I do not have ids on checkboxes).

response_body.should have_selector 'input[type=radio][checked=checked][value=information]'

Explanation: test will return true if body of document contains an radio button (input[type=radio]) which is checked and that has the value "information"

一身软味 2024-08-03 22:01:11
input("#my_box").should be_checked
input("#my_box").should be_checked
四叶草在未来唯美盛开 2024-08-03 22:01:10
expect(find_field("radio_button_name")).to be_checked
expect(find_field("radio_button_name")).to be_checked
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文