Poltergeist 不可能,您只需检查标题即可。
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/
我的方法 - 在使用 Spinach 和 Rubyzip 时略有不同:
将以下内容添加到您的Gemfile
group :test do
gem 'chromedriver-helper' # for Chrome <= 28
gem 'chromedriver2-helper' # for Chrome >= 29
gem 'selenium-webdriver'
end
features/support/capybara.rb - 我将 Poltergeist 用于带有@javascript
标签的场景,将 Chrome 用于带有@download
标签的场景。
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
功能/支持/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
features/steps/file_download.rb - 请注意,您不能使用page.response_headers
它,因为 Selenium/ChromeDriver 不支持它。但是您可以使用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