0
votes

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.

4
Can you update the question with the modal as well pls.Ziyan Junaideen
@Ziyan Junaideen what is modal?vini91

4 Answers

0
votes

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.

0
votes

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'
0
votes

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'
0
votes

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.