Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
27 lines (22 sloc) 893 Bytes
layout title tags published
post
Capybara attach_file on Windows
Capybara
attach_file
rails
Windows
true

Capybara 2.4.4 will fail silently on Windows when trying to use an otherwise valid (Unix) file path with attach_file.

Based on slash and backslash in Ruby, I put the following in my spec_helper.rb:

USING_WINDOWS = !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) || (RUBY_PLATFORM=~ /mswin|mingw/))

In some_file_upload_spec.rb:

file_to_attach = File.join(Rails.root, 'spec/fixtures/files/cool_avatar.jpg')
if USING_WINDOWS
    file_to_attach = file_to_attach.gsub('/', '\\')
end
page.attach_file('file_input_dom_id', file_to_attach)