Below is my form, you can see that there will be many rows of inputs fields all with the same classes:
<%= q.simple_fields_for :choices do |c|%>
<div class="row-fluid choice-row">
<%= c.text_field :sort_order, class: 'span1' %>
<%= c.text_field :title, class: 'span9' %>
<%= c.link_to_remove 'Remove', class: 'btn btn-danger span2 pull-right' %>
</div>
<% end %>
For my tests I'm trying:
fill_in(:sort_order, with: '3')
fill_in(:title, with: 'Choice 3')
I've also tried:
fill_in('Sort Order', with: '3')
fill_in('Title', with: 'Choice 3')
And:
fill_in('Sort Order', :with => '3')
fill_in('Title', :with => 'Choice 3')
The error I get is:
Failure/Error: fill_in(:title, with: 'Choice 3')
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'title' found
Can someone please recommend how I can fill a specific input, like say the third row?
You'll find in your markup that fields output by a form-helper are namespaced within the object passed to fields_for, this indeed the entire point of using fields_for.
In this case, your form elements will have names like choices[title] and choices[sort_order].
To find and fill_in the correct field when selecting by name yields many fields, you need to get more creative, because this is not a very typical setup:
You can use within to select a unique parent element
You can use CSS selectors to find the nth element
You can find all elements and use [2] on the returned array to access the 3rd element
Related
Having this form:
<h3 class="contact-header"><span>Ready to try it</span> GET IN TOUCH</h3>
<%= form_for #contact, url: pages_path, remote: true do |f| %>
<div class= "flex-row">
<div class="form-input-spacing">
<%= f.label :name %>
<%= f.text_field :name, required: true, class: "contact-form-text-area" %>
</div>
<div class="form-input-spacing">
<%= f.label :email %>
<%= f.text_field :email, required: true, class: "contact-form-text-area" %>
</div>
</div>
<div class="area-input">
<%= f.label :message %>
<%= f.text_area :message, rows: 8, cols: 59, required: true, class: "contact-form-text-area",
placeholder: "Send us a message"%>
</div>
<div class="submit-form">
<%= f.submit 'Send Message', class: 'btn btn-primary' %>
</div>
<% end %>
I am trying to test how it would be filled and then sent with Capybara:
scenario "visitor/user sends incomplete contact form" do
visit root_path #form is in root
within(".new_contact") do
# fill_in "#contact_name", with: "Test User"
# fill_in "contact_name", with: "Test User"
find("#contact_name").set("Test User")
end
save_and_open_page
end
I have tried js: true in the scenario, and both fill_in and find but when I do save_and_open_page nothing is filled.
The reason why I used .new_contact is because in the Inspector, thats the class that the form takes, the #contact_nameis the id that the input takes andcontact_nameis the labelfor`.
If I use click_button "Send Message" the button is clicked and a message appears so why is it getting the button but not the input? Thanks!
save_and_open_page saves the HTML with the element attributes, not the element's properties. It is really only useful for when you want to see modified page structure (the HTML structure has been changed). When you change the content of a field it changes the value property of that element but doesn't actually update the page HTML (value attribute), so when you save the HTML it won't have the value set. If you want to see the page as it was use save_and_open_screenshot, or just pause your test and look at the browser.
Also fill_in takes a locator, not CSS, so it would be
fill_in "contact_name", with: "Test User"
from https://rubydoc.info/gems/capybara/Capybara/Node/Actions#fill_in-instance_method - The field can be found via its name, id, test_id attribute, placeholder, or label text. - not CSS
I'm perplexed about an error message that I am receiving from my feature spec regarding a missing capybara element.
I have a simple form in my new.html.erb as shown below:
<h1>Register a Building</h1>
<h4 class="notice"></h4>
<%= simple_form_for #building do |f| %>
<%= f.input :street_address %>
<%= f.input :city %>
<%= f.input :state, collection: Building::STATES %>
<%= f.input :postal_code %>
<%= f.input :description %>
<%= f.button :submit %>
<% end %>
My feature_spec is testing for the registration of a new building with the required fields as shown below:
scenario 'I want to submit a valid building to record' do
building_count = Building.count
visit new_building_path
fill_in 'Street address', with: '5555 Summer Drive'
fill_in 'City', with: 'Boston'
fill_in ' State', with: 'WA'
fill_in 'Postal code', with: '78526'
fill_in 'Description', with: ''
click_on 'Create Building'
expect(page).to have_content('Building Submitted')
expect(page).to have_content('')
expect(Building.count).to eql(building_count + 1)
end
Everything is fine up until it reaches the state field. I keep getting an element not found error, but when I do a save and open page it is CLEARLY there.
Here is my error:
Failure/Error: fill_in 'State', with: 'WA'
Capybara::ElementNotFound:
Unable to find field "State"
I've noticed the double/single quotes in the error message but when I match the quotes and change them nothing happens. Also, I've inspected my element and I get that the title is this " State". I've changed my test to look for " State" not "State" but still to no avail. What can I do to fix or get around this?
Your State field is a select box not a text field.
Use:
select 'WA', from: 'State'
fill_in specifically looks for the field as fillable, i.e. text fields and text areas.
The Capybara docmentation about this is here: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions
In my application a user can search for a zipcode which gives back its corresponding street name and city. The zipcode is written in a <%= text_field_tag %> while the street name and city would be put into a div.
Now I have changed it that the street name would also be rendered into a <%= text_field_tag %> but my spec is now failing.
The code below is a working test where the streetname and city would be put into a div.
it 'submits the zipcode and house number', js: true do
fill_in 'zipcode', with: '1111AA'
fill_in 'house_number', with: 1
click_button('Search')
within('#street_name') do
page.should have_content 'Street X'
end
within('#city') do
page.should have_content 'Town X'
end
end
My divs:
<div id="street_name"><%= street_name %></div>
<div id="city"><%= city %></div>
My text field tags:
<%= text_field_tag 'street_name', street_name, placeholder: 'Straatnaam', id: 'street_name' %>
<%= text_field_tag 'city', city, placeholder: 'Woonplaats', id: 'city' %>
How should I write my spec so it checks that the streetname and city are in a <%= text_field_tag %> instead of a div?
I'm pretty sure that you can find your text field and make an assertion about it's value with the following code:
find_field("street_name").value.should eq 'Street X'
Make sure, though, that the id is unique across the layout, otherwise you can run into some nasty ambiguity failures.
I hope that helps !
I have a text field like this :
<%= form_for(ownership, remote: true) do |f| %>
<div>
<%= f.text_field :confirm, value: nil, title: t('label.transaction.confirmation.code') %>
<%= f.hidden_field :start_date, value: Time.now %>
</div>
<%= f.submit t('button.ownership.take.confirmation'), class: "btn btn-small"%>
<% end %>
And I want to fill in the text field with rspec :
fill_in t('label.transaction.confirmation.code'), with: "something"
But it doesn't work because the rspec don't recognize the title tag in order to fill in the text field :
Failure/Error: fill_in t('label.transaction.confirmation.code'), with: confirmation_code
Capybara::ElementNotFound:
Unable to find field "Code de confirmation"
Do you know a way to fill in the text field with rspec without adding a label ?
the fill_in method's first argument must be a css selection. for example: #id, li, .class.
So you can change t('label.transaction.confirmation.code')
to the text field id or [title='#{t('label.transaction.confirmation.code')}']
e.g.
<input id="conf_code">
fill_in 'conf_code', with: 'something'
I have a question about forms. I have a fairly standard form that saves a post (called an eReport in my app) with a title and body. The table also has a "published" field, which is boolean. The saved eReport only shows on the public site if this field is set to true, with false being the default.
Rather than the default check box, I would like to display two buttons at the end of the form: a "Publish Now" button and a "Save as Draft" button. If the user presses the former, the published field would be set to true. If the latter, then false. In PHP, I used to display 2 submit fields with different name values, then handle the input with an if/else statement to determine the proper SQL query to build. In Rails, I'm assuming I would place this logic in the controller, under the appropriate action, but I'm not sure how to manipulate the name or id values of buttons.
For the record, I'm using Formtastic, but if someone could show me how to do this with the default Rails form tags, that's OK too. Here's the code for my form as it stands right now:
<% semantic_form_for #ereport do |form| %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
<% form.buttons do %>
<%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
<% end %>
<% end %>
Thanks in advance for the help!
I don't know about formtastic, but with the default rails form builder, you could do it like this:
<%= form.submit "Save with option A", :name => "save_option_a" %>
<%= form.submit "Save with option B", :name => "save_option_b" %>
Then in the controller, you can pick those up in params:
if params[:save_option_a]
# do stuff
end
in addition to #iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)
Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}
where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.
which was obtained by submit_tag "Confirmar", :name => 'comentar'
So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...
and retrieve them in your controller
if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end
I think you need to use jQuery.
You can bind the button click event and submit the form for specified location.