【问题标题】:How can I determine if a radio button is checked already?如何确定单选按钮是否已被选中?
【发布时间】:2010-11-13 12:39:26
【问题描述】:

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

【问题讨论】:

    标签: radio-button cucumber webrat checked


    【解决方案1】:
    expect(find_field("radio_button_name")).to be_checked
    

    【讨论】:

      【解决方案2】:
      input("#my_box").should be_checked
      

      【讨论】:

      • 这个方法好像不行了。我正在使用水豚 2.4.1。 @evedovelli 建议的 find_field 方法成功了。
      • 我建议不要使用“should be_checked”,而是:expect(find_field("my_boxt")).to be_checked
      【解决方案3】:

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

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

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

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

      【讨论】:

        【解决方案4】:

        刚刚将 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
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:
                  And the "Obvious choice" checkbox should be checked
              

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

              【讨论】:

                【解决方案8】:

                你可以在你的领域使用checked?方法

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-01-11
                  • 2012-06-18
                  • 2020-12-04
                  • 2015-10-31
                  • 1970-01-01
                  相关资源
                  最近更新 更多