自动化Capybara从已检查的单选按钮中提取值

时间:2016-03-03 09:12:49

标签: ruby rspec automation automated-tests capybara

我正在寻找一种方法来检查选择的无线电,并将该选项的值保存到变量中。 一旦我有变量,我想构建一个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是选中的选项(选中=&#34;选中&#34;) 我想提取选项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

提前致谢!

2 个答案:

答案 0 :(得分:0)

如果选中&#39; option_3_id`,您可以通过

找到其值
find(:radio_button, 'option_3_id', checked: true).value

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

答案 1 :(得分:0)

由于您希望在一组中选中已检查的无线电的值,因此您希望使用共享名称和已选中的过滤器

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