Gjaldon
Gjaldon

Reputation: 5644

How to test JQuery-file-upload with RSpec and Capybara

I have implemented a JQuery-file-upload in my Rails4 app. File upload works when I manually test it from the browser, but my test for it fails.

Below is my spec for the JQuery-file-upload: require 'spec_helper'

feature 'Evidences' do
  context "as an assessor user" do
    let!(:assessor) { User.make! :assessor }
    let!(:assessment)  { Assessment.make! }

    background { sign_in assessor }

    scenario "it uploads evidence", js: true do
      evidences_count_before_upload = assessment.evidences.count
      visit edit_assessment_path(assessment)

      path = "#{Rails.root}/spec/fixtures/files/sample1.doc"
      attach_file 'evidence_file_url', path

      expect(assessment.evidences.count).to eq(evidences_count_before_upload + 1)
    end
  end
end

I'm using RSpec 2, Capybara 2 and Poltergeist for this feature spec.

Upvotes: 7

Views: 3363

Answers (3)

Markus
Markus

Reputation: 5807

This is a problem with poltergeist. Using another driver (for example selenium-webdriver) should resolve the issue. It appears that file upload events are not triggerred correctly with poltergeist.

(I have some jquery ajax file upload tests and the same problems: My file upload tests only do not work with poltergeist)

There is an closed issue where someone describes the same symptomps. But no clear solution.

Upvotes: 0

McFadden
McFadden

Reputation: 1630

I am also using JQuery-file-upload with RSpec and Capybara. I am using the capybara-webkit driver, but this should work with selenium as well.

See the example method and usage found in this answer: https://stackoverflow.com/a/11203629/1084109

Upvotes: 1

trueunlessfalse
trueunlessfalse

Reputation: 1233

Although you didn't say how / why your test fails: A common problem here is that the file-field is hidden, so capybara treats it as not there.

A solution that worked for me is to execute a script to show the file field, something along the lines of:

script = "$('input[type=file]').show();"
page.driver.browser.execute_script(script)

You might also need to hide your custom upload button / label. See also: Capybara, capybara-webkit and custom file upload form

Upvotes: 0

Related Questions