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
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 trying to solve a challenge, but I received an error message from Cabybara saying:
`Failure/Error: fill_in 'Name', with: 'Vostro 2017'
Capybara::ElementNotFound: Unable to find visible field "Name" that is not disabled`
My new.html.erb is:
<%= form_for #item, url: {action: "create"} do |f|%>
<%= f.label 'Name' %>
<%= f.text_field :name %>
<%= f.label 'Description' %>
<%= f.text_field :description %>
<%= f.label 'Features' %>
<%= f.text_field :features %>
<%= f.label 'Asset number' %>
<%= f.text_field :assetNumber %>
<%= f.submit%>
<% end %>
And my item_controller.rb is:
class ItemController < ApplicationController
def show
#items = Item.find(params[:id])
end
def new
#item = Item.new
end
def create
#item = Item.new(item_params)
#item.save
redirect_to #item
end
private
def item_params
params.require(:item).permit(:name, :description, :features, :assetNumber)
end
end
The rspec file that is being used to do the test is:
require 'rails_helper'
feature 'User creates a new inventory item' do
scenario 'successfully' do
visit new_item_path
fill_in 'Name', with: 'Vostro 2017'
fill_in 'Description', with: 'Dell Notebook'
fill_in 'Features', with: '16gb, 1Tb, 15.6"'
fill_in 'Asset number', with: '392 DLL'
click_button 'Create Item'
expect(page).to have_content 'Vostro 2017'
expect(page).to have_content 'Dell Notebook'
expect(page).to have_content '16gb, 1Tb, 15.6"'
expect(page).to have_content '392 DLL'
end
end
I'm using ruby-2.3.5 and rails 4.1.0.
I'm beginner in ruby/rails and I can't figure out what's wrong with my code.
Could somebody help me to solve that?
I appreciate in advance.
You could do this, assuming your input has an id of name:
find("input[id$='name']").set "Vostro 2017"
or:
find("#name").set "Vostro 2017"
You might also try lower-casing Name:
fill_in 'name', with: "Vostro 2017"
Capybara will target the name or the id attribute, so the second example should work.
Rails generates form input names with using form object.
fill_in 'item[name]', with: "Vostro 2017"
fill_in 'item[description]', with: 'Dell Notebook'
fill_in 'item[features]', with: '16gb, 1Tb, 15.6"'
fill_in 'item[assetNumber]', with: '392 DLL'
If you look at the actual HTML of the page instead of the erb template (always better to include the HTML unless your question is specifically about the erb) you'll notice your labels aren't actually associated with the input elements (no matching for attribute to the id of the input). Obviously for Capybara to find the element by label text ('Name' in your case) the label would have to be correctly associated with the element. To fix that you need to use f.label - http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label - correctly. If you want to specify the text of the element (vs using text extracted from i18n translation) that would be
f.label :name, 'Name'
I realized what I was doing wrong, so let's go:
I changed the instance variable items inside def show action to item (not pluralized) and I changed an atribute from assetNumber to asset_number, so this way the Cabybara test could understand correctly.
Thanks guys.
Trying to test some functionality in a simple page and I am getting this error when using page.check:
Failures:
1) searching for stores serving a taco and a sauce: Adobada tacos with Chile de Arbol sauce
Failure/Error: page.check 'Adobada'
Capybara::ElementNotFound:
Unable to find checkbox "Adobada"
# ./spec/views/store_search_spec.rb:8:in `block (2 levels) in <top (required)>'
Here is my HTML:
<%= simple_form_for :stores, url: stores_path, method: :get do |f| %>
<%= f.label :tacos, 'Select Tacos to Find: ', class: 'label' %>
<div class='taco-select checkboxes'>
<%= f.collection_check_boxes :tacos, Taco.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>
<%= f.label :salsa, 'Select Sauces to Find: ', class: 'label' %>
<div class='salsa-select checkboxes'>
<%= f.collection_check_boxes :salsas, Salsa.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>
<%= f.submit 'Search!' %>
<% end %>
And this is my test:
require 'rails_helper'
feature 'searching for stores', %(serving a taco and a sauce:) do
scenario 'Adobada tacos with Chile de Arbol sauce' do
visit root_path
page.check 'Adobada'
page.check 'Chile de Arbol'
click_button 'Search!'
expect(page).to have_content "Store"
expect(page).to have_content 'City'
end
end
I would like to test that when some checkboxes are set to true a certain content is rendered in the page.
Don't know how to fix this.
Try with these, they should work for you:
find(:css, "#YOUR_CHECKBOX_ID").set(true)
or
page.check('YOUR_CHECKBOX_ID')
or simply
find('THE CSS SELECTOR OF YOUR CHECKBOX').click
find(:xpath, 'THE XPATH OF YOUR CHECKBOX').click
You don't have to use an ID to check a checkbox. You can just use the label on the checkbox. Say you have a checkbox like this
<label for="...">
<input type="checkbox" name="...">Something</label>
You 'check' it in capybara like so
it "" do
page.check 'Something'
end
Official docs on this can be found here
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'