I would like to select radio button from an excel spreadsheet for a data driven test.
I have tried the following:
radio=worksheet.cells(rows,"A").value
browser.radio(:name => 'name').set radio
I also tried selecting as a string but got errors:
radio=worksheet.cells(rows,"A").value
string_from_excel = "set"
browser.send string_from_excel, radio
the code for the radio button is:
<input id="radio" type="radio" onclick="setradio(0)" value="0" name="radio">
Thanks for the help
First lets try to make your code a bit easier to read. Notice all the questions in the comments and other answers? It's not clear what you are retrieving from the spreadsheet, and the naming of the variable that holds that data 'radio' just presents yet another instance of that term in a section of code (and associated html) that is littered with that word already. Call it something like radio_id
, radio_value
or radio_name
to make it clear to anyone what should getting retrieved from the spreadsheet, and what exactly that variable contains. It will also make more sense to yourself 4 months from now.
Second, carefully evaluate how to properly identify the item you need. Your sample HTML only shows a single radio control, but usually these controls come in sets, and most often the NAME is used to identify radio controls that are in a common set, see the examples on this page. Using the name attribute is therefore quite often a terrible way to identify a radio button. Value, or the embedded text is usually a far better way of getting the 'right' radio control out of the set.
Thirdly, the .set
method for a watir radio button object does not accept a parameter, it merely ensures the particular radio control specified is in the 'set' state. The complement to .set
is .clear
(see the Watir RDOC for more info)
Also Your control has a event handler defined in it in the HTML. This is executing a script when the control receives an onclick event. So after setting it (or perhaps instead of setting it) in order for the page to work correctly, you may need to fire the onclick event so that the proper client side code is called.
With that in mind I'd try code that looks something like this
radio_value = worksheet.cells(rows,"A").value
browser.radio(:value => radio_value).set
browser.radio(:value => radio_value).fire_event('onclick')
Soapbox:
Another reason radio's come in sets is that you cannot 'un-set' a radio button with your mouse except by picking another button in the same set. You almost never see a single radio button standing alone in a web-page. If a single thing that can be selected or unselected is needed, a 'checkbox' element is usually used. If a developer is using a single radio as a 'checkbox' by implementing javascript code to make it behave like a checkbox, they need (IMHO) to be taken out and shot sacked and replaced with a developer who knows how to use CSS for such things instead of abusing the poor radio control and creating a page that will not work properly if someone has javascript disabled.
First, change the name from 'name' to 'radio', then just use set
browser.radio(:name => 'radio').set