Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
downloads.rb
module Downloads
module_function
def download_dir
ENV["TEST_ENV_NUMBER"] ? "downloads/#{ENV["TEST_ENV_NUMBER"]}" : "downloads"
end
def downloads_path
File.join(Capybara.save_path, download_dir)
end
def downloads
Dir["#{downloads_path}/*"]
end
def last_download
downloads.max_by { |f| File.mtime(f) }
end
def last_download!
sleep 5 # This is required, otherwise capybara reads an empty file.
wait_until(wait_time: 10) { last_download && last_download !~ /\.crdownload\z/ }
last_download
end
def download(name)
open(File.join(Capybara.save_path, download_dir, name))
end
end
RSpec.configure do |config|
config.before :each do
FileUtils.rm_rf Downloads.downloads_path
FileUtils.mkdir_p Downloads.downloads_path
end
config.include Downloads
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment