2017-04-11 16 views
-1

こんにちは私はrspec capybaraテストを実行しようとするといくつかの問題が発生し、レールの初心者です。Capybara :: ElementNotFound:ファイルフィールド "file"を見つけることができません

require 'rails_helper' 

describe "Upload Process", :type => :feature do 

    it "Can upload a file" do 
    visit new_document_path 
    page.attach_file('file', '/Users/yaomin/Desktop/my_travel_pic/uploadtest.jpg', visible: false) 
    click_button 'Upload' 
    page.should have_content("Uploadtest") 
    end 
end 

しかし、私は私の見解コードは以下の

h1 Listing documents 

table 
    thead 
    tr 
     th 
     th 
     th 

    tbody 
    - @documents.each do |document| 
     tr 
     td = link_to 'Show', document 
     td = link_to 'Edit', edit_document_path(document) 
     td = link_to 'Destroy', document, data: { confirm: 'Are you sure?' }, method: :delete 

br 

= link_to 'New Document', new_document_path 

は、新しいドキュメントビューである

Failures: 

    1) Upload Process Can upload a file 
    Failure/Error: attach_file('file', '/Users/yaomin/Desktop/my_travel_pic/uploadtest.jpg', visible: false) 

    Capybara::ElementNotFound: 
     Unable to find file field "file" 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/finders.rb:44:in `block in find' 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/base.rb:85:in `synchronize' 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/finders.rb:33:in `find' 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/actions.rb:256:in `attach_file' 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/session.rb:769:in `block (2 levels) in <class:Session>' 
    # /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>' 
    # ./spec/features/upload_test.rb:9:in `block (2 levels) in <top (required)>' 

Finished in 0.40034 seconds (files took 1.99 seconds to load) 1 example, 1 failure 

Failed examples: 

rspec ./spec/features/upload_test.rb:5 # Upload Process Can upload a file 

エラーを得た

h1 New document 

= render 'form' 

= link_to 'Back', documents_path 

- if @document.folder 
    = link_to "Back to '#{@document.folder.name}' Folder", browse_path(@document.folder) 
- else 
    = link_to "Back", root_url 

部分私のフォーム3210
= form_for @document do |f| 
    - if @document.errors.any? 
    #error_explanation 
     h2 = "#{pluralize(@document.errors.count, "error")} prohibited this document from being saved:" 
     ul 
     - @document.errors.full_messages.each do |message| 
      li = message 
    = f.label :file 
    = f.file_field :file 
    - f.hidden_field :folder_id 

    = f.submit "Upload" 

実際にはファイルを手動でアップロードできますが、アップロードテストは失敗します。すべてのあなたの助けを事前に

感謝:)

+0

あなたのビューコードを教えてください。そして 'ファイルをアップロードできます'の中で 'save_and_open_page'を実行すると、実際のファイルフィールドが見えますか? –

+0

ビューと部分形式で更新 – NYM

+0

保存して開いているページがうまく動作します – NYM

答えて

0

ID、名前、または関連するラベルのテキストのいずれかであることをattach_fileニーズに渡される最初のパラメータ。あなたの場合は、おそらく 'ファイル'ではありません。あなたが関連するラベルのテキストは非常におそらく「ファイルの選択」

attach_file('Choose File', file_path) 

のようなものである述べることは、あなたが望むものである(最初のパラメータは、実際のHTMLせずにする必要があります正確に何を言うことは不可能)。仕事のできる他の事は

attach_file('document_file', file_path) # match the elements id 
attach_file('document[file]', file_path) # match the elements name 

だろうが、もう一度、最初のパラメータは、HTMLは、彼らがどうあるべきかを表示します、わずかに異なる可能性があります。

ファイル入力に共通するその他の問題は、ブラウザ間で同じスタイルを適用するために、CSSで非表示にすることができることです(透過的にするなど)。そのような場合は、あなたはJSが可能なドライバを使用しているあなたは、フィールドが表示されるようにしようと

attach_file('Choose File', file_path, make_visible: true) 

のようなものを使用することができた場合は、ファイルを設定し、元のCSSを復元します。

+0

の回答で推測したものです。こんにちは、返信いただきありがとうございます。 – NYM

+0

@NYMあなたはあなたが思っているページ(new_document_pathにはログインする必要がありますか?など)ではないか、htmlはあなたの考えではありません。 'attach_file'を呼び出す前に' page.html'を見て、あなたの質問にフォームHTMLを追加してください。 –

関連する問題