I keep getting this error:
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Morning' found.
I've reset spork, done a full db reset, tried assigning an ID to the form element, etc. What could possibly be the issue here?
days_controller_spec.rb
require 'spec_helper'
describe DaysController do
describe "New" do
describe "with valid information" do
it "should create a new entry" do
visit 'days#new'
fill_in "Morning", with: "Test"
click_button "Submit"
end
end
end
end
days_controller.rb
<%= form_for #day do |f| %>
<%= f.label :morning %>
<%= f.text_field :morning %>
<%= f.button :submit %>
<% end %>
Looks like you app is using JavaScript. With capybara you need to add :js => true option to the block that's dealing with JS pages.
Try:
it "should create a new entry", :js => true do
You may also need to way for the form to be rendered before trying to fill_in the field.
Also, I recommend you check out capybara's integration DSL. Read more about it here
Turns out that syntax was all correct, but the issue was that the test was in the wrong RSpec spec file. When I swapped this test into an integration_test file, it worked perfectly.
Related
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.
I could not resolve this issue. Please help me. It gives me element not found error.
spec/features/todos/create_spec.rb
require 'spec_helper'
describe "Creating todos" do
let(:user) { FactoryGirl.create(:user)}
before(:each) do
visit root_path
click_link "Login"
fill_in "Email", with: "user#gmail.com"
fill_in "Password", with: "password"
click_button "Sign in"
end
it "redirects to the todos index page" do
visit "/todos"
fill_in "title", with: "MyString"
click_button "Create"
expect(page).to have_title("MyString")
end
my view code.
_new_form.html.erb
<%= form_for current_user.todos.new, remote:true do |f| %>
<div class="input-group">
<%= f.text_field :title, class: "form-control", placeholder: "title" %>
<span class="input-group-btn">
<%= f.submit "Create", class: "btn btn-success" %>
</span>
</div>
<% end %>
spec_helper.rb
def sign_in
#user = FactoryGirl.create(:user)
controller.stub(:authenticate_user!).and_return(true)
#user
end
If you have the code running on a server locally, inspect the element and the name of the field is what Capybara needs to run. Generally, if the form is nested, the name rails will come up with (in this case) is something like todos[title].
So, spec/features/todos/create_spec.rb should look something like:
require 'spec_helper'
...
it "redirects to the todos index page" do
visit "/todos"
fill_in "todos[title]", with: "MyString"
click_button "Create"
expect(page).to have_title("MyString")
end
Found my answer.
In the view file I've a text_field with title with an id. The id = todo_title in the console
But am calling title in the test . Here the capybara not been able to find title. It was todo_title
After using
fill_in "todo_title"
It worked like charm.
In your form, there must be an element with the name as "title". From the docs:
fill_in(locator, options = {}) ⇒ Object
Locate a text field or text area and fill it in with the given text The field can be found via its name, id or label text.
I have the following form and I want to check if the text field is present or not. How can I do that ?
<%= form_for(ownership, remote: true) do |f| %>
<div>
<%= f.text_field :confirm, value: nil %>
<%= f.hidden_field :start_date, value: Time.now %>
</div>
<%= f.submit t('button.ownership.take.confirmation'), class: "btn btn-small"%>
<% end %>
Here my test :
describe "for not confirmed ownership" do
before do
FactoryGirl.create(:agreed_ownership, user: current_user, product: product)
be_signed_in_as(current_user)
visit current_page
end
# it { should_not have_text_field(confirm) }
it { should_not have_button(t('button.ownership.take.confirmation')) }
end
You'd use a has_css? expectation:
it "should have the confirm input field" do
visit current_page
expect(page).to have_css('input[type="text"]')
end
You can use additional jQuery-style selectors to filter for other attributes on the input field, too. For example, 'input[type="text"][name*="confirm"]' would select for confirm appearing in the input field's name attribute.
To set an expectation the field isn't present, you'd use to_not on your expectation: expect(page).to_not have_css('input[type="text"]')
Bonus: Here's the older, should-style syntax:
it "should have the confirm input field" do
visit current_page
page.should have_css('input[type="text"]')
end
it "shouldn't have the confirm input field" do
visit current_page
page.should_not have_css('input[type="text"]')
end
I'm currently writing a test for a form using RSpec, and I was wondering how I'd go about selecting a radio button, given the form below:
<%=form_for(#whatever) do|f|%>
<%=f.label :option, "TRUE" %>
<%=f.radio_button :option, true %>
<%=f.label :option, "FALSE" %>
<%=f.radio_button :morning, false %>
<%=f.submit "SAVE" %>
<% end %>
I want my test to look something like:
describe "with valid options selected" do
before do
#CODE TO SELECT A RADIO BUTTON
click_button "SAVE"
end
end
describe "with valid info" do
before do
choose('True')
click_button "Create Setlist"
end
...
end
I had to assign an ID to the radio button for this to work as well
Doc for additional reference: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions
I'm writing a few basic step instructions in Cucumber, and can't figure out why it's throwing up a "method missing" error. Here's the precise error:
undefined method `question' for nil:NilClass (ActionView::Template::Error)
The root of this error is this line from my index.html.erb page:
<p id="question"><%= #question.question %></p>
So it looks like the test thinks that #question is nil. I'm not sure why it thinks this. What should I change in my step instructions? Here they are:
Given /^a question$/ do
#question = Question.create!(:question => "here's my question..", :answer => "right_answer", :num => "1")
end
When /^I answer the question$/ do
visit root_path
fill_in "Answer", :with => "Wrong answer"
click_button "Submit answer"
end
The first step instruction passes, but the second does not.
EDIT: Here's my entire index.html.erb page, in case it's useful:
<% title "Questions" %>
<p id="question"><%= #question.question %></p>
<%= form_for #user_answer do |f| %>
<div><%= f.label :answer %></div>
<div><%= f.text_field :answer %></div>
<div id="sign_up_button"><%= f.submit "Submit answer" %></div>
<% end %>
<p><%= link_to "New Question", new_question_path %></p>
EDIT: My Question model, in case it's useful:
class Question
include MongoMapper::Document
key :question, String, :required => true
key :answer, String, :required => true
key :num, String, :required => true
Assuming you're giving us the excerpt from the stack trace properly, your issue is that you aren't setting #question properly in your controller - which you have not posted above. Several other people have alluded to that in their answers, but I'm quite convinced of it.
Your tests - cucumber or otherwise - have nothing to do with the problem you're having.
I suspect that if you visit your root_url, you will get the same problem - you will get a stack trace and an error. Without knowing exactly what your routes are, I can't be sure where you're being sent when you visit the root_url.
Your controller should be loading up #question with your question object (probably via Question.find() or something like it), then rendering your index.html.erb page. So your 'index' action in your controller ought to be the thing that is filling in your #question.
Note that you're not following rest-ey principles here - usually people have 'indexes' give you lists of things (questions in your instance), then id's give you a particular question.
I bet if you go to a particular question it works fine.
Maybe your problem is that you shouldn't be testing by visiting 'root_path' in the first place? Maybe you should be going to a particular "question's" path?
Do you have any validations at Question model? Maybe you can't create new question object like you trying.