Skip to content

Instantly share code, notes, and snippets.

@snehaso
Created May 4, 2020 07:48
Show Gist options
  • Save snehaso/e11498d4abe03fcd19dc8cff6bb60762 to your computer and use it in GitHub Desktop.
Save snehaso/e11498d4abe03fcd19dc8cff6bb60762 to your computer and use it in GitHub Desktop.
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