|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The SFC licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +module Selenium |
| 21 | + module WebDriver |
| 22 | + module DriverExtensions |
| 23 | + module HasFileDownloads |
| 24 | + def downloadable_files |
| 25 | + verify_enabled |
| 26 | + |
| 27 | + @bridge.downloadable_files['names'] |
| 28 | + end |
| 29 | + |
| 30 | + def download_file(file_name, target_directory) |
| 31 | + verify_enabled |
| 32 | + |
| 33 | + response = @bridge.download_file(file_name) |
| 34 | + contents = response['contents'] |
| 35 | + |
| 36 | + File.open("#{file_name}.zip", 'wb') { |f| f << Base64.decode64(contents) } |
| 37 | + target_directory += '/' unless target_directory.end_with?('/') |
| 38 | + FileUtils.mkdir_p(target_directory) |
| 39 | + |
| 40 | + begin |
| 41 | + Zip::File.open("#{file_name}.zip") do |zip| |
| 42 | + zip.each { |entry| zip.extract(entry, "#{target_directory}#{file_name}") } |
| 43 | + end |
| 44 | + ensure |
| 45 | + FileUtils.rm_f("#{file_name}.zip") |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def delete_downloadable_files |
| 50 | + verify_enabled |
| 51 | + |
| 52 | + @bridge.delete_downloadable_files |
| 53 | + end |
| 54 | + |
| 55 | + private |
| 56 | + |
| 57 | + def verify_enabled |
| 58 | + return if capabilities['se:downloadsEnabled'] |
| 59 | + |
| 60 | + raise Error::WebDriverError, 'You must enable downloads in order to work with downloadable files.' |
| 61 | + end |
| 62 | + end # HasFileDownloads |
| 63 | + end # DriverExtensions |
| 64 | + end # WebDriver |
| 65 | +end # Selenium |
0 commit comments