From 2af9ccddf1dd7e48c29b136d1ac88f5161def1f0 Mon Sep 17 00:00:00 2001 From: Sayed Abdelhaleem Date: Fri, 29 Jan 2016 14:33:14 +0200 Subject: [PATCH] Improve photo export download integration test closes #6663 --- features/desktop/download_photos.feature | 23 +++++++++----- features/step_definitions/user_steps.rb | 8 +++++ features/support/download_helpers.rb | 40 ++++++++++++++++++++++++ features/support/env.rb | 14 ++++++++- 4 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 features/support/download_helpers.rb diff --git a/features/desktop/download_photos.feature b/features/desktop/download_photos.feature index 5bdad21f1..8a9e63a07 100644 --- a/features/desktop/download_photos.feature +++ b/features/desktop/download_photos.feature @@ -1,11 +1,20 @@ -@wip @javascript Feature: Download Photos - Scenario: Download my photos - Given I am signed in - And I click on my name in the header - And I follow "settings" + Scenario: Request my photos + Given I am signed in + When I click on my name in the header + When I follow "Settings" Then I should be on my account settings page - And I follow "download my photos" - Then I confirm the alert + When I follow "Request my photos" + Then I should see a flash message indicating success + And I should see a flash message containing "We are currently processing your photos" + + Scenario: Download my photos + Given I am signed in + When I did request my photos + And I click on my name in the header + When I follow "Settings" + Then I should be on my account settings page + When I follow "Download my photos" + Then I should get a zipped file diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index b23e37597..7442c926e 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -219,3 +219,11 @@ end When /^I click the sign in button$/ do click_link "Sign in" end + +Given /^I did request my photos$/ do + @me.perform_export_photos! +end + +Then /^I should get a zipped file$/ do + expect(DownloadHelpers.download).to end_with("zip") +end diff --git a/features/support/download_helpers.rb b/features/support/download_helpers.rb new file mode 100644 index 000000000..a3fb9fd49 --- /dev/null +++ b/features/support/download_helpers.rb @@ -0,0 +1,40 @@ +# Credits goes to Steve Richert +# http://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver/ +module DownloadHelpers + TIMEOUT ||= 5 + PATH ||= Rails.root.join("tmp/downloads") + + module_function + + def downloads + Dir[PATH.join("*")] + end + + def download + wait_for_download + downloads.first + end + + def download_content + wait_for_download + File.read(download) + end + + def wait_for_download + Timeout.timeout(TIMEOUT) do + sleep 0.1 until downloaded? + end + end + + def downloaded? + !downloading? && downloads.any? + end + + def downloading? + downloads.grep(/\.part$/).any? + end + + def clear_downloads + FileUtils.rm_f(downloads) + end +end diff --git a/features/support/env.rb b/features/support/env.rb index 838d32dea..99e84a7a5 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -21,7 +21,17 @@ Rails.application.routes.default_url_options[:port] = AppConfig.pod_uri.port Selenium::WebDriver::Firefox::Binary.path = ENV["FIREFOX_BINARY_PATH"] || Selenium::WebDriver::Firefox::Binary.path Capybara.register_driver :selenium do |app| - Capybara::Selenium::Driver.new(app, browser: :firefox) + profile = Selenium::WebDriver::Firefox::Profile.new + # Set the download directory to "tmp/downloads" + profile["browser.download.dir"] = DownloadHelpers::PATH.to_s + # Save the file instead of opening it + profile["browser.download.folderList"] = 2 + # Hide the download Manager + profile["browser.download.manager.showWhenStarting"] = false + # Suppress "open with" dialog for zipped files only + profile["browser.helperApps.neverAsk.saveToDisk"] = "application/zip" + # Start Firefox using our profile + Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile) end Capybara.register_driver :mobile do |app| @@ -70,4 +80,6 @@ include HelperMethods Before do Devise.mailer.deliveries = [] + # Delete all files in "tmp/downloads" + DownloadHelpers.clear_downloads end