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
4116 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
import pytest
22
23
from selenium.webdriver.common.by import By
24
from selenium.webdriver.support.wait import WebDriverWait
25
26
27
@pytest.mark.no_driver_after_test
28
def test_get_downloadable_files(driver, pages):
29
_browser_downloads(driver, pages)
30
file_names = driver.get_downloadable_files()
31
32
assert "file_1.txt" in file_names
33
assert "file_2.jpg" in file_names
34
assert type(file_names) is list
35
36
37
@pytest.mark.no_driver_after_test
38
def test_download_file(driver, pages):
39
_browser_downloads(driver, pages)
40
41
# Get a list of downloadable files and find the txt file
42
downloadable_files = driver.get_downloadable_files()
43
text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
44
assert text_file_name is not None, "Could not find a .txt file in downloadable files"
45
46
with tempfile.TemporaryDirectory() as target_directory:
47
driver.download_file(text_file_name, target_directory)
48
49
target_file = os.path.join(target_directory, text_file_name)
50
with open(target_file) as file:
51
assert "Hello, World!" in file.read()
52
53
54
@pytest.mark.no_driver_after_test
55
def test_delete_downloadable_files(driver, pages):
56
_browser_downloads(driver, pages)
57
58
driver.delete_downloadable_files()
59
assert not driver.get_downloadable_files()
60
61
62
def _browser_downloads(driver, pages):
63
pages.load("downloads/download.html")
64
driver.find_element(By.ID, "file-1").click()
65
driver.find_element(By.ID, "file-2").click()
66
WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files())
67
68