Andy Clarke
Andy Clarke

Reputation: 23

Automation Capybara extract value from checked radio button

I am looking for a way to check what radio is selected and save the value of that option into a variable. once i have the variable i want to build a switch statement which will give me the logic to decide what to do.

here is the 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) 

option 3 is the checked option (checked="checked") I want to extract the value of option 3 (option_3) and save it in a variable

default_option = #the code that extracts the 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

Thanks in advance!

Upvotes: 2

Views: 605

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49870

Since you want the value of the checked radio in a group of them you want to use the shared name and the checked filter

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

Upvotes: 1

fabdurso
fabdurso

Reputation: 2444

In the case the 'option_3_id` is checked, you can find its value by

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

But as you don't know which radio button is checked, you should first find all the options and verify which one is currently checked (checked:true/false), and then retrieve its value.

Upvotes: 0

Related Questions