How to test JQuery-file-upload with RSpec and Capybara
Asked Answered
M

3

7

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.

Midian answered 23/8, 2013 at 8:28 Comment(0)
R
1

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://mcmap.net/q/619263/-using-selenium-to-imitate-dragging-a-file-onto-an-upload-element

Raisin answered 14/3, 2014 at 23:45 Comment(0)
H
0

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

Homemaking answered 29/8, 2013 at 10:53 Comment(0)
A
0

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.

Apostil answered 3/7, 2015 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.