cuprite icon indicating copy to clipboard operation
cuprite copied to clipboard

Allow to pass save_path to driver

Open stoivo opened this issue 3 years ago • 1 comments

I want to specify where filed are being downloaded. Defaulting to Capybara.save_path is probably an okay default but then the downloads and screenshots are mixed.

With this change you can pass save_path to driver initiation.

Capybara::Cuprite::Driver.new(app, save_path: "tmp/capybara/downloads")

An alternative would be to manually send the command to move download path.

driver.browser.page.command(
  "Page.setDownloadBehavior",
  behavior: "allow",
  downloadPath: "tmp/capybara/simons"
)

related to #181

We use it for spec and have a helper module which looks like this. Would you like to include it in cuprite? I think not since you probably want downloads for this tab/page not all downloads

# source:  https://stackoverflow.com/a/29544674
module DownloadHelpers
  TIMEOUT = 5
  PATH    = Rails.root.join("tmp/capybara/downloads")

  module_function

  def list_downloads
    Dir[PATH.join("*")]
  end

  def only_download
    list_downloads
      .tap { |downloads| raise <<~ERROR if downloads.size > 1 }
        DownloadHelpers#only_download excpects there to only be one download,
        maybe someone isn't clearing downloads after their spec?
        DownloadHelpers.clear_downloads will do the trick
      ERROR
      .first
  end

  def only_download_content
    wait_for_download
    File.read(only_download)
  end

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

  def downloaded?
    !downloading? && list_downloads.any?
  end

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

  def clear_downloads
    FileUtils.rm_f(list_downloads)
  end
end

stoivo avatar May 31 '22 08:05 stoivo

Whoops, seams like a duplicate of #192

stoivo avatar May 31 '22 08:05 stoivo

Just FYI I've bumped #192 to https://github.com/rubycdp/cuprite/pull/217/files 🙇

singhprd avatar Nov 15 '22 15:11 singhprd

I'll merge #192 thanks!

route avatar Nov 15 '22 17:11 route