How to check a radio button in cucumber? - ruby-on-rails

I'm using cucumber with RoR (with either webrat or capybara)
How can I write a step to check a radio button? I've tried "choose" or "select" but it can't find my radio button.
I'm not sure what to do, as I have in fact 2 inputs with the same name (the 2 radio buttons belonging to the same "group")
Thanks
Example of html
<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">
<input type="radio" name="group1" value="Milk"> Milk<br>
<input type="radio" name="group1" value="Butter" checked> Butter<br>
<input type="radio" name="group1" value="Cheese"> Cheese
</form>

The answer is to choose the id (generated by Rails) of the radio button.
<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">
<input type="radio" name="group1" value="Milk" id="group1_milk"> Milk<br>
<input type="radio" name="group1" value="Butter" checked id="group1_butter"> Butter<br>
<input type="radio" name="group1" value="Cheese" id="group1_cheese"> Cheese
</form>
and do
choose("group1_milk").
That will work even if more radio buttons have the same options.

In your step definition add line:
choose('A Radio Button')
Cucumber uses Capybara, you can read more about it here: https://github.com/jnicklas/capybara

Related

Prevent user to type in a textbox [duplicate]

what is the code to disable an INPUT text box for HTML?
Thanks
<input type="text" disabled="disabled" />
See the W3C HTML Specification on the input tag for more information.
<input type="text" required="true" value="" readonly="true">
This will make a text box in readonly mode, might be helpful in generating passwords and datepickers.
The syntax to disable an HTML input is as follows:
<input type="text" id="input_id" DISABLED />
You can Use both disabled or readonly attribute of input . Using disable attribute will omit that value at form submit, so if you want that values at submit event make them readonly instead of disable.
<input type="text" readonly>
or
<input type="text" disabled>
<input type="text" required="true" value="" readonly>
Not the.
<input type="text" required="true" value="" readonly="true">

Capybara choose first occurrence of label

I have the following radio buttons:
<input type="radio" value="0" /> Correct
<input type="radio" value="1" /> Wrong
<input type="radio" value="2" /> Wrong
<input type="radio" value="3" /> Wrong
I'm trying to use Capybara to select the first input with label "Wrong". From the documentation, it seems like this should work:
choose first "Wrong"
But I get an error message:
Ambiguous match, found 4 elements matching radio button nil
Help?
The first needs to be the value of the match option. The match option defines the matching algorithm (see the readme for all available options).
The statement should look like:
choose "Wrong", :match => :first

Submitting a collection of checkboxes in MVC when the first checkbox is unchecked produces null

Basically I have a collection of strings, and I want to render them as checkboxes on the page. To do this, I have written this code:
#for (var i=0; i< Model.AvailableCats.Length; i++)
{
<input type="checkbox" name="Cats[#i]" value="#Model.Cats[i]" #(Model.Cats.Contains(Model.AvailableCats[i]) ? "checked=checked" : "") /> #Model.AvailableCats[i]
}
This produces checkboxes like
<input type="checkbox" checked="checked" value="Bengal" name="Cats[0]">
<input type="checkbox" checked="checked" value="Moggy" name="Cats[1]">
When submitted this works ok if both are checked, or if the first is checked, but if only the 2nd item is checked, it's only submitting Cats[1] and MVC does not map this into an array.
I'm sure the answer is very simple but how can I submit my collection of checkbox values?
This is why Html.Checkbox actually adds a hidden input element in addition to the checkbox input. Checkboxes only submit a value if they are checked, so adding a hidden input with the same name, means that if it's not checked, something will be submitted, even if it's only an empty string.
<input type="checkbox" checked="checked" value="Bengal" name="Cats[0]">
<input type="hidden" name="Cats[0]" value="">
<input type="checkbox" checked="checked" value="Moggy" name="Cats[1]">
<input type="hidden" name="Cats[1]" value="">
Okay the answer was simple, just have the same name property on each one. It doesn't have to be unique and MVC binds it into a collection if more than one are submitted.
<input type="checkbox" checked="checked" value="Bengal" name="Cats">
<input type="checkbox" checked="checked" value="Moggy" name="Cats">
Surprised it took me that long.

Handling radio buttons groups select event with JQuery UI

why JQuery UI option buttons (buttonset) doesn't have any events - I want to handle select events for it, wonder what's the right way for doing that.
You should just tap into the normal change event for the radio buttons themselves. Expanding on the example on jQueryUI's website:
Html:
<div id="radio">
<input type="radio" id="radio1" name="radio" value="1" /><label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radio" value="2" checked="checked" /><label for="radio2">Choice 2</label>
<input type="radio" id="radio3" name="radio" value="3" /><label for="radio3">Choice 3</label>
</div>
JavaScript:
$("#radio").buttonset();
$("input[name='radio']").on("change", function () {
alert(this.value);
});
The buttonset widget is just styling the radio buttons a certain way. All of the regular events will fire as expected.
Example: http://jsfiddle.net/andrewwhitaker/LcJGd/

Radio button value check in action is true or false

I am new in MVC. I am working on a view where I add two radio buttons.
<label for="lDIV1">
<input id="lDIV1" type="radio" name='rbtab' value='DIV1' onclick="javascript:custom()" />Create
Email:</label>
<label for="lDIV2">
<input id="lDIV2" type="radio" name='rbtab' checked="checked" value='DIV2' onclick="javascript:defaul()" />Default
Email:</label>
<div id='Content' style="display: block">
Now I want to call a function in Action {Http} that is if first radio button is true then call radiobutton1() method or else call radiobutton2() method
Can you guys Help me out how to apply conditions..
im not sure what you want to do because your question is not that clear but here is how you can do something when a checkbox is checked with jquery
http://jsfiddle.net/HQXLm/1/
<input id="checkme" type="checkbox" /> click me to find out if im checked
$('#checkme').click(function(){
if ($(this).is(':checked')){
alert('I am checked, do something here');
}
});

Resources