【问题标题】:Automation Capybara extract value from checked radio button自动化 Capybara 从选中的单选按钮中提取值
【发布时间】:2016-06-16 12:23:36
【问题描述】:

我正在寻找一种方法来检查选择的收音机并将该选项的值保存到变量中。 一旦我有了变量,我想构建一个 switch 语句,它会给我决定做什么的逻辑。

这里是 HTML:

<input type="radio" value="option_1" name="registration[registrationType]" id="option_1_id" class="radio">
        (Option 1)
<input type="radio" value="option_2" name="registration[registrationType]" id="option_2_id" class="radio">
        (Option 2)
<input type="radio" value="option_3" name="registration[registrationType]" id="option_3_id" class="radio" checked="checked">
        (Option 3) 

选项 3 是选中的选项 (checked="checked") 我想提取选项 3 (option_3) 的值并将其保存在变量中

default_option = #提取默认选项的代码

case default_option
when "option_1"
  puts 'lets do something with this'
when "option_2"
 puts 'lets do something with this'
when "option_3"
 puts 'lets do something with this'
else
  puts 'cant do much with this'
end

提前致谢!

【问题讨论】:

  • `find(:radio_button, 'option_3_id', checked: true).value' 返回什么?
  • 这将返回正是我想要的 ID。谢谢
  • 我会添加一个更完整的答案安迪
  • 如果有帮助,请接受它;)并欢迎使用stackoverflow!

标签: ruby rspec automation automated-tests capybara


【解决方案1】:

由于您想要一组中选中单选的值,因此您希望使用共享名称和选中的过滤器

find(:radio_button, 'registration[registrationType]', checked: true).value

【讨论】:

    【解决方案2】:

    如果'option_3_id`被选中,你可以找到它的值

    find(:radio_button, 'option_3_id', checked: true).value
    

    但由于您不知道选中的是哪个单选按钮,您应该首先找到所有选项并验证当前选中的是哪个(checked:true/false),然后检索其值。

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多