使用Capybara和Poltergeist驱动程序将文件下载到特定文件夹 [英] Downloading file to specific folder using Capybara and Poltergeist driver

查看:41
本文介绍了使用Capybara和Poltergeist驱动程序将文件下载到特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Capybara和Poltergeist驱动程序编写验收测试。我需要验证下载的CSV文件的内容。

I am writing my acceptance tests using Capybara and Poltergeist driver.I need to validate the content of the CSV file downloaded.


  1. I尝试了多种方法来呈现页面本身上的内容,而不是下载内容。

  2. 也尝试更改了mime类型,但无法正常工作。

最后,我想选择将文件下载到特定文件夹中,然后使用核心ruby库读取CSV文件,以解决此问题。

Finally I want to settle down with the option of downloading the file in a specific folder and then read the CSV file using core ruby libraries.

为了实现这一点,当poltergeist驱动程序单击下载链接时,我希望它处理弹出窗口,然后直接在给定文件夹中下载文件。

In order to achieve this,when poltergeist driver clicks on download link then I want it to handle the pop-up and download the file directly in the given folder.

在Selenium的chrome和firefox驱动程序中,我可以选择配置配置文件以处理弹出窗口并配置下载目录。

In Selenium's chrome and firefox drivers, I have option of configuring profiles to handle pop ups and configure download directory.

使用poltergeist是否有这样的选择?任何信息都会有所帮助。

Is there any such option using poltergeist? Any information will be helpful.

推荐答案

使用Poltergeist是不可能的,您只需检查标题即可。

It is not possible with Poltergeist, you can just check the headers.

 step 'I should get zipped file' do
    page.response_headers['Content-Disposition'].should include("filename=\"file.zip\"")
 end

但是可以使用Chrome驱动程序以及最新版本的Firefox和Selenium Webdriver。不幸的是,它是通过Selenium运行的-即不是无头的...请参阅本文: http://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver/

But is is possible with Chrome driver and also with recent versions of Firefox and Selenium Webdriver. Unfortunately it runs via Selenium - i.e. not headless... See this article: http://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver/

我的方法-与我使用Spinach和Rubyzip稍有不同:

My approach - slightly different as I'm working with Spinach and Rubyzip:

将以下内容添加到您的 Gemfile中

Add the following to your Gemfile

group :test do
  gem 'chromedriver-helper'  # for Chrome <= 28
  gem 'chromedriver2-helper' # for Chrome >= 29
  gem 'selenium-webdriver'
end

功能/support/capybara.rb -我正在将Poltergeist用于 @javascript 标签和带有 @download 标签的Chrome浏览器。

features/support/capybara.rb - I'm using Poltergeist for scenarios with @javascript tag and Chrome for scenarios with @download tag.

require 'spinach/capybara'
require 'capybara/poltergeist'
require 'selenium/webdriver'

# ChromeDriver 1.x, for Chrome <= 28 
Capybara.register_driver :chrome do |app|
  profile = Selenium::WebDriver::Chrome::Profile.new
  profile['download.default_directory'] = DownloadHelper::PATH.to_s
  args = ["--window-size=1024,768"]
  Capybara::Selenium::Driver.new(app, browser: :chrome, profile: profile, args: args)
end

# ChromeDriver 2.x, for Chrome >= 29 
Capybara.register_driver :chrome do |app|
  prefs = {
    download: {
      prompt_for_download: false,
      default_directory: DownloadHelper::PATH.to_s
    }
  }
  args = ['--window-size=1024,768']
  Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs, args: args)
end

# Tested with Firefox 27 and Selenium Webdriver 2.39
Capybara.register_driver :firefox do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['browser.download.dir'] = DownloadHelper::PATH.to_s
  profile['browser.download.folderList'] = 2 # 2 - save to user defined location
  profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/zip'
  Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end

Capybara.javascript_driver = :poltergeist # :webkit :selenium :poltergeist :chrome

Spinach.hooks.on_tag("javascript") do
  Capybara.current_driver = Capybara.javascript_driver
  Capybara.default_wait_time = 5
end

Spinach.hooks.on_tag("download") do
  Capybara.current_driver = :chrome # or :firefox
  Capybara.default_wait_time = 50
end

功能/support/downloads.rb

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

  extend self

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

  def download_path
    wait_for_download
    downloads.first
  end

  def download_content
    wait_for_download
    File.read(download_path)
  end

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

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

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

  def clear_downloads
    FileUtils.rm_f(downloads)
  end

end

Spinach.hooks.before_scenario do |scenario|
  DownloadHelper.clear_downloads
end

Spinach.hooks.after_scenario do
  DownloadHelper.clear_downloads
end

功能/file_download.feature

Feature: File download
   As a user
   I want to be able to download my files

Background:
  Given I am logged in as a user
  And I have uploaded files in the system

@download
Scenario: Successfull download
  When I click on the download button
  Then I should get zipped files

功能/步骤/file_download.rb -请注意,您不能使用 page.response_headers ,因为Selenium / ChromeDriver不支持它。但是您可以使用 File.basename()检查下载文件的文件名。

features/steps/file_download.rb - Note that you can't use page.response_headers as it is not supported by the Selenium/ChromeDriver. But you can check the filename of the downloaded file using the File.basename().

class Spinach::Features::FileDownload < Spinach::FeatureSteps
  include SharedAuthentication

  step 'I click on the download button' do
    click_link "Download"
  end

  step 'I should get zipped files' do
    File.basename(DownloadHelper.download_path).should == 'file.zip'
    Zip::ZipFile.open(DownloadHelper.download_path) do |zipfile|
      zipfile.find_entry('myfile.txt').should_not be_nil
      zipfile.find_entry('myphoto.jpg').should_not be_nil
    end
  end

end

这篇关于使用Capybara和Poltergeist驱动程序将文件下载到特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆