Path: blob/trunk/py/test/selenium/webdriver/remote/remote_downloads_tests.py
4116 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import os18import tempfile1920import pytest2122from selenium.webdriver.common.by import By23from selenium.webdriver.support.wait import WebDriverWait242526@pytest.mark.no_driver_after_test27def test_get_downloadable_files(driver, pages):28_browser_downloads(driver, pages)29file_names = driver.get_downloadable_files()3031assert "file_1.txt" in file_names32assert "file_2.jpg" in file_names33assert type(file_names) is list343536@pytest.mark.no_driver_after_test37def test_download_file(driver, pages):38_browser_downloads(driver, pages)3940# Get a list of downloadable files and find the txt file41downloadable_files = driver.get_downloadable_files()42text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)43assert text_file_name is not None, "Could not find a .txt file in downloadable files"4445with tempfile.TemporaryDirectory() as target_directory:46driver.download_file(text_file_name, target_directory)4748target_file = os.path.join(target_directory, text_file_name)49with open(target_file) as file:50assert "Hello, World!" in file.read()515253@pytest.mark.no_driver_after_test54def test_delete_downloadable_files(driver, pages):55_browser_downloads(driver, pages)5657driver.delete_downloadable_files()58assert not driver.get_downloadable_files()596061def _browser_downloads(driver, pages):62pages.load("downloads/download.html")63driver.find_element(By.ID, "file-1").click()64driver.find_element(By.ID, "file-2").click()65WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files())666768