Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions py/test/selenium/webdriver/common/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import re
import threading

import filetype

try:
from urllib import request as urllib_request
except ImportError:
Expand Down Expand Up @@ -70,8 +72,20 @@ def _serve_page(self, page_number):

def _serve_file(self, file_path):
"""Serve a file from the HTML root directory."""
with open(file_path, encoding="latin-1") as f:
return f.read().encode("utf-8")
with open(file_path, "rb") as f:
content = f.read()

kind = filetype.guess(content)
if kind is not None:
return content, kind.mime

# fallback for text files that filetype can't detect
if file_path.endswith(".txt"):
return content, "text/plain"
elif file_path.endswith(".json"):
return content, "application/json"
else:
return content, "text/html"

def _send_response(self, content_type="text/html"):
"""Send a response."""
Expand All @@ -89,8 +103,7 @@ def do_GET(self):
self._send_response("text/html")
self.wfile.write(html)
elif os.path.isfile(file_path):
content_type = "application/json" if file_path.endswith(".json") else "text/html"
content = self._serve_file(file_path)
content, content_type = self._serve_file(file_path)
self._send_response(content_type)
self.wfile.write(content)
else:
Expand Down
22 changes: 6 additions & 16 deletions py/test/selenium/webdriver/remote/remote_downloads_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
def test_get_downloadable_files(driver, pages):
_browser_downloads(driver, pages)
file_names = driver.get_downloadable_files()
# TODO: why is Chrome downloading files as .html???
# assert "file_1.txt" in file_names
# assert "file_2.jpg" in file_names
assert any(f in file_names for f in ("file_1.txt", "file_1.htm", "file_1.html"))
assert any(f in file_names for f in ("file_2.jpg", "file_2.htm", "file_2.html"))

assert "file_1.txt" in file_names
assert "file_2.jpg" in file_names
assert type(file_names) is list


Expand All @@ -42,12 +40,8 @@ def test_download_file(driver, pages):

# Get a list of downloadable files and find the txt file
downloadable_files = driver.get_downloadable_files()
# TODO: why is Chrome downloading files as .html???
# text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
text_file_name = next(
(f for f in downloadable_files if all((f.endswith((".txt", ".htm", ".html")), f.startswith("file_1")))), None
)
assert text_file_name is not None, "Could not find file in downloadable files"
text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
assert text_file_name is not None, "Could not find a .txt file in downloadable files"

with tempfile.TemporaryDirectory() as target_directory:
driver.download_file(text_file_name, target_directory)
Expand All @@ -69,8 +63,4 @@ def _browser_downloads(driver, pages):
pages.load("downloads/download.html")
driver.find_element(By.ID, "file-1").click()
driver.find_element(By.ID, "file-2").click()
# TODO: why is Chrome downloading files as .html???
# WebDriverWait(driver, 5).until(lambda d: "file_2.jpg" in d.get_downloadable_files())
WebDriverWait(driver, 5).until(
lambda d: any(f in d.get_downloadable_files() for f in ("file_2.jpg", "file_2.htm", "file_2.html"))
)
WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files())