Sha256: 0f084ef7bd50070011838a0089e04567ec4156f398422f1eadba91386e94ac8b

Contents?: true

Size: 976 Bytes

Versions: 50

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

module DownloadHelper
  TIMEOUT = 10
  PATH = Rails.root.join("tmp/downloads").freeze

  def downloads(name = nil)
    Dir[PATH.join(name || "*")]
  end

  def download_path(name = nil)
    wait_for_download(name)
    downloads(name).first
  end

  def download_content(name = nil)
    wait_for_download(name)
    File.read(download_path(name))
  end

  def wait_for_download(name = nil)
    Timeout.timeout(TIMEOUT) do
      sleep 0.1 until downloaded?(name)
    end
  end

  def downloaded?(name = nil)
    downloads(name).any? && !downloading?
  end

  def downloading?
    downloads.grep(/\.crdownload$/).any?
  end

  def clear_downloads
    FileUtils.rm_f(downloads)
  end
end

RSpec.configure do |config|
  config.include DownloadHelper, download: true
  config.before :each, download: true do
    FileUtils.mkdir_p DownloadHelper::PATH.to_s
    page.driver.browser.download_path = DownloadHelper::PATH.to_s
    clear_downloads
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
decidim-dev-0.29.1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.4 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.9 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.29.0 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.3 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.8 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.29.0.rc4 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.29.0.rc3 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.29.0.rc2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.29.0.rc1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.7 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.6 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.10 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.9 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.0 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.5 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.0.rc5 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.28.0.rc4 lib/decidim/dev/test/rspec_support/z_download_helper.rb