Capybara choose first occurrence of label - capybara

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

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">

Rails capybaraAmbiguous match, found 50 elements matching css

I get this error:
Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"
When I put the following code:
find('input[value="delete"]').first.click
On html file:
<div id="deletediv-38">
<form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/38">
<input type="hidden" name="_method" value="delete" />
<input type="submit" name="delete-38" value="delete" />
</form>
</div>
<div id="deletediv-39">
<form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/39">
<input type="hidden" name="_method" value="delete" />
<input type="submit" name="delete-39" value="delete" />
</form>
</div>
.
.
.
find('input[id="delete-38"]') doesn't work , it says element not found.
And I can't count as well.
when I do
find('input[value="delete"]').count.should_be > 0
I again get the error:
Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"
To explain the errors you're having -
find will find one unique element, if there are none or more than one matching you will get an error. Your find('input[id="delete-38"]') fails because according to the html the only elements with ids are divs and have divs of the format deletediv-xx. If you were trying to find the inputs with the name of delete-38 you would do
find('input[name="delete-38"]')
As #dimakura stated to get the first element you use #first. If you want a count of the elements you need to use #all - like so
all('input[value="delete"]').count
If the page is dynamically changing and you want to know the count on the page once at least one has appeared then you could do
all('input[value=delete]', minimum: 1).count
which would use Capybaras waiting behavior for at least one to appear before returning (or it times out while waiting)
Try using this instead:
first('input[value="delete"]').click

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.

How to check a radio button in cucumber?

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

MVC Razor String Concat

I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks
I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />
another option:
<input type="checkbox" name="#(Id + "_chk")" />

Resources