title: Testing javascript click function capybara attach file image upload
body: <p>I'm trying to test a photo upload paperclip using Capybara. However I'm getting an error about file field when running cucumber test.</p>
  
  <pre><code>Unable to find file field :upload (Capybara::ElementNotFound)
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>$("#uploadhere").click(function() {
      $("#photo_upload_entry_upload").click();
    });
  </code></pre>
  
  <p>Steps.rb file</p>
  
  <pre><code>Then(/^I should see photo when I upload and submit entry$/) do
    script = "$('form.new_photo_upload_entry').css('i.fa.fa-file-image-o');"
    page.execute_script(script)
  
    fixture_path = Rails.root.join('spec', 'support', 'fixtures', 'test.jpg')
  
    within('form.new_photo_upload_entry') do 
      attach_file(:upload, fixture_path)
    end
  end
  </code></pre>
  
  <p>HTML (using inspect element)</p>
  
  <pre><code>&lt;input type="file" name="photo_upload_entry[upload]" id="photo_upload_entry_upload"&gt;
  </code></pre>
  
  <p>Ruby code of form in slim format</p>
  
  <pre><code>.entry-label STEP 1: UPLOAD YOUR IMAGE
      .entry-upload#uploadhere
        .upload-here
          i.fa.fa-file-image-o
          br
          = "UPLOAD YOUR IMAGE HERE"
        img
      =f.file_field :upload
  </code></pre>