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.26.8 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.4 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.3 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.7 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.5 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.4 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.0 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.3 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.0.rc2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.27.0.rc1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.0 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.0.rc2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.26.0.rc1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.25.2 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.25.1 lib/decidim/dev/test/rspec_support/z_download_helper.rb
decidim-dev-0.25.0 lib/decidim/dev/test/rspec_support/z_download_helper.rb