Skip to content

A platform for community discussion. Free, open, simple.

1.2 KB
Raw Blame
# 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.join("_unused")

        zip_path =
          page.driver.with_playwright_page do |pw_page|
            pw_page.expect_download { click_link ".zip" }.path
          end

        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