Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/remote/remote_downloads_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import os
19
import tempfile
20
21
from selenium.webdriver.common.by import By
22
from selenium.webdriver.support.wait import WebDriverWait
23
24
25
def test_get_downloadable_files(driver, pages):
26
_browser_downloads(driver, pages)
27
28
file_names = driver.get_downloadable_files()
29
30
assert "file_1.txt" in file_names
31
assert "file_2.jpg" in file_names
32
assert type(file_names) is list
33
34
35
def test_download_file(driver, pages):
36
_browser_downloads(driver, pages)
37
38
# Get a list of downloadable files and find the txt file
39
downloadable_files = driver.get_downloadable_files()
40
text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
41
assert text_file_name is not None, "Could not find a .txt file in downloadable files"
42
43
with tempfile.TemporaryDirectory() as target_directory:
44
driver.download_file(text_file_name, target_directory)
45
46
target_file = os.path.join(target_directory, text_file_name)
47
with open(target_file) as file:
48
assert "Hello, World!" in file.read()
49
50
51
def test_delete_downloadable_files(driver, pages):
52
_browser_downloads(driver, pages)
53
54
driver.delete_downloadable_files()
55
assert not driver.get_downloadable_files()
56
57
58
def _browser_downloads(driver, pages):
59
pages.load("downloads/download.html")
60
driver.find_element(By.ID, "file-1").click()
61
driver.find_element(By.ID, "file-2").click()
62
WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files())
63
64