Fill in a field without label - ruby-on-rails

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'

Related

Form is not being filled - Capybara

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

Capybara unable to find field "search"

I tried setting the id in various parts of the form, as well as wrapping the form in a div with id set to search. The result is always the same.
spec/features/02_post_spec.rb
scenario 'search for post title' do
fill_post_form(post3)
fill_in "search", with: post3.title
click_button("Search")
expect(page).to_not have_content(post1.title)
expect(page).to_not have_content(post2.title)
expect(page).to have_content(post3.title)
end
spec/spec_helper.rb
def fill_post_form(post)
visit new_post_path
fill_in 'Title', with: post.title
fill_in 'Body', with: post.body
click_button 'Create Post'
end
This will redirect_to post_path(post)
views/posts/index.html.erb
<%= form_tag(posts_path, method: :get, class: "block") do %>
<div class="form-group">
<div class="input-group">
<%= text_field_tag :search, params[:search], class: "form-control", id: "search" %>
<span class="input-group-btn">
<%= submit_tag "Search", name: nil, class: "btn btn-default" %>
</span>
</div>
</div>
<% end %>
Capybara output
Failure/Error: fill_in "search", with: post3.title
Capybara::ElementNotFound:
Unable to find field "search"
The command fill_in "search", with: post3.title you're using should work, assuming you are on the page rendering the shown partial, and the output of that partial is actually visible on the page (not hidden via CSS). Your scenario doesn't show visiting an actual page, and you don't show what fill_post_form is doing so it's tough to know exactly what is going wrong. First step would be to do something like
fill_post_form(post3) # already in your tests
sleep 2 # wait a few seconds to make sure above method has completed whatever actions it does
puts page.html # output the current page and take a look to see if your 'search' element is actually on the page

Capybara fill_in multiple input fields

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

Capybara Conundrum, field cannot be found

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

Capybara error when trying to fill a text_field_tag

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 !

Resources