简体   繁体   中英

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM