Skip to content

Instantly share code, notes, and snippets.

@VoltanBro
Last active February 7, 2023 09:28
Show Gist options
  • Save VoltanBro/d83284ca54892467e56c9aa6b0447e90 to your computer and use it in GitHub Desktop.
Save VoltanBro/d83284ca54892467e56c9aa6b0447e90 to your computer and use it in GitHub Desktop.
rspec download helper
module DownloadHelper
TIMEOUT = 3
PATH = Rails.root.join("tmp/downloads")
module_function
def downloads
Dir[PATH.join("*")]
end
def download
downloads.first
end
def download_content
wait_for_download
File.read(download)
end
def wait_for_download
Timeout.timeout(TIMEOUT) do
sleep 0.3 until downloaded?
end
end
def downloaded?
!downloading? && downloads.any?
end
def downloading?
downloads.grep(/\.crdownload$/).any?
end
def clear_downloads
FileUtils.rm_f(downloads)
end
end
RSpec.configure do |config|
config.include DownloadHelper, type: :feature
end
RSpec.configure do |config|
config.before(:each) do |example|
Capybara.current_driver = JS_DRIVER if example.metadata[:js]
Capybara.current_driver = :selenium if example.metadata[:selenium]
Capybara.current_driver = :selenium_chrome if example.metadata[:selenium_chrome]
DownloadHelper.clear_downloads
end
config.after(:each) do
Capybara.use_default_driver
DownloadHelper.clear_downloads
end
end
it "downloads document with codes" do
visit edit_codes(code)
click_on("Export codes")
wait_for_download
expect(downloads.length).to eq(1)
expect(download).to match(/.*\.csv/)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment