Capybara doesn't attach file


I’m trying to pass a feature test for uploading an image.

my form looks like this:

<%= form_for [:admin, @research_project] do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title %>
    <%= f.label :body %>
    <%= f.text_area :body %>
    <%= f.label :image %>
     <%= f.file_field :image %>
     <%= f.collection_check_boxes :research_theme_ids, ResearchTheme.all, :id, :title %>
  <% if @research_project.new_record? %>
     <%= f.submit "Add Research Project" %>
  <% else %>
    <%= f.submit "Update Research Project" %>
  <% end %>
<% end %>

my feature test looks like this:

  scenario 'Admin adds a research project' do
    click_link "Add Research Project"
    fill_in 'Title', with: @research_project.title 
    fill_in 'Body', with: @research_project.body 
    attach_file 'Image', "spec/support/uploads/monk_large.jpg"
    click_button "Add Research Project"
    }.to change(ResearchProject, :count).by(1)

    expect(page).to have_content @research_project.title 
    expect(page).to have_content "you successfully added a new research project"

    visit admin_research_project_path
    expect(page).to have_css 'img', text: "monk_large.jpg"

when I run the test, I get this failure message, indication there is an img element but with no value for the src attribute:

1) Admin interacts with research projects Admin adds a research project
     Failure/Error: expect(page).to have_css 'img', text: "monk_large.jpg"
       expected to find css "img" with text "monk_large.jpg" but there were no matches. Also found "", which      matched the selector but not all filters.

Can’t figure out why Capybara won’t attach the file. I put the test image in the folder: /spec/support/uploads

Any help is more then welcome

my repo is at:



Shouldn’t it be src: "monk_large.jpg instead of text: "monk_large.jpg" ?

Hi Andy,

I tried you’re suggestion but apperently :src is not a supported key:

Failure/Error: expect(page).to have_css 'img', src: "monk_large.jpg"
   invalid keys :src, should be one of :text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait

thanks for your help anyway,


Try this instead:

expect(page).to have_css("img[src='monk_large.jpg']")

no, doesn’t work. When I check the page with the Launch command ‘save_and_open_page’, there is no image on the page. I don’t understand this, because if I do a ‘sanity test’, the image is there.

In another project, I used this feature test for testing image uploads, and it worked fine:

feature 'Admin adds new video' do
  scenario 'Admin successfully adds a new video' do
    admin = Fabricate(:admin)
    dramas = Fabricate(:genre, name: "Dramas")
    visit new_admin_video_path

    fill_in "Title", with: "Monk"
    select "Dramas", from: "Genre"
    fill_in "Description", with: "SF detective"
    attach_file "Large cover", "spec/support/uploads/monk_large.jpg"
    attach_file "Small cover", "spec/support/uploads/monk.jpg"
    fill_in "Video URL", with: ""

    click_button "Add Video"


    visit video_path(Video.first)
    expect(page).to have_selector("img[src='/my-flix/monk_large.jpg']")
    expect(page).to have_selector("a[href='']")

for the project I’m currently working on, I’m doing exactly the same, but the test fails.

So at the moment I don’t know if I’m missing a Capybara configuration somewhere, or if something has changed in Capybare 2.3.0

And does it work for you in the browser? I mean can you attach file to this form?

yes, in the browser everything works fine, I’m able to select an image and upload it.