2017-10-25 11 views
0

私は挑戦を解決しようとしているが、私は言ってCabybaraからエラーメッセージを受信:のRails:カピバラ:: ElementNotFound

`Failure/Error: fill_in 'Name', with: 'Vostro 2017' 
    Capybara::ElementNotFound: Unable to find visible field "Name" that is not disabled` 

マイnew.html.erbは次のとおりです。

<%= 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 %> 

そして、私のitem_controller.rbされます:

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 

テストを行うために使用されているRSpecのファイルは次のとおりです。

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 

私はruby-2.3.5とrails 4.1.0を使用しています。 私はRuby/Railsの初心者です。私のコードで何が間違っているのか分かりません。 誰かがそれを解決するのに手伝ってもらえますか? 私は前もって感謝します。

+0

質問をモーダルでもplsでも更新できますか? –

+0

@Ziyan Junaideenモーダルとは何ですか? – viniCdaSilva

答えて

0

あなたがこれを行うことができ、あなたの入力はnameのIDを持っていると仮定すると:

find("input[id$='name']").set "Vostro 2017" 

か:

find("#name").set "Vostro 2017" 

をあなたはまた、より低ケーシングNameを試してみてください:

fill_in 'name', with: "Vostro 2017" 

Capybaraは名前またはid属性を対象とするため、2番目の例では機能するはずです。

+0

テストファイルを変更した場合、チャレンジの所有者は気に入らないでしょう。 – viniCdaSilva

0

Railsは、フォームオブジェクトを使用してフォーム入力名を生成します。

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

あなたは(あなたの質問は、ERBについて、具体的でない限り、常により良いHTMLを含むように)ページの実際のHTMLの代わりに、ERBテンプレートを見れば、あなたのラベルが実際に関連付けられていませんがわかります入力要素(入力のidに一致するno属性for)。明らかに、カピバラがラベルテキスト(あなたの場合は「名前」)で要素を見つけるためには、ラベルは要素に正しく関連付けられなければなりません。これを修正するには、f.label - http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-labelを正しく使用する必要があります。あなたは私が間違って何をやっていたが実現

f.label :name, 'Name' 
+0

素晴らしいチップ。どうもありがとう。 – viniCdaSilva

0

だろう(I18N翻訳から抽出されたテキストを使用して対)要素のテキストを指定し、そのようにしたい場合の行く: 私はデフ内部のインスタンス変数の項目を変更しました(複数化されていない)項目へのアクションを表示し、assetNumberからasset_numberへの属性を変更しました。このように、Cabybaraテストは正しく理解できました。

ありがとうございます。

関連する問題