discourse/spec/system/page_objects/pages/csv_export_pm.rb
Jarek Radosz 6aa8f1838b
DEV: Partially restore download logic in specs (#39783)
A follow-up to 2d640d58ad.

There was a small regression, where the download directory has changed,
resulting in spec artifact archive pollution. This change keeps all the
improvements from the previous commit, but restores the original save
path.
2026-05-06 11:03:19 +02:00

53 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module PageObjects
module Pages
class CSVExportPM < PageObjects::Pages::Base
def initialize
super
@downloaded_files = []
end
def download_and_extract
original_save_path = Capybara.save_path
Capybara.save_path = Downloads::FOLDER
zip_name = find("a.attachment").text
zip_path = File.join(Downloads::FOLDER, zip_name)
@downloaded_files << zip_path
page.driver.with_playwright_page { |pw_page| pw_page.expect_download { click_link ".zip" } }
Downloads.wait_for(zip_path)
csv_path = unzip(zip_path).first
@downloaded_files << csv_path
CSV.read(csv_path)
ensure
Capybara.save_path = original_save_path
end
def clear_downloads
@downloaded_files.each { |file| FileUtils.rm_f(file) }
@downloaded_files = []
end
def has_download_link?
find(:link, ".zip").present?
end
private
def unzip(file)
paths = []
Zip::File.open(file) do |zip_file|
zip_file.each do |f|
f.extract(f.name, destination_directory: Downloads::FOLDER)
paths << File.join(Downloads::FOLDER, f.name)
end
end
paths
end
end
end
end