From decd0b8a2673b9bd4f0eec73640677298537e36a Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 8 Dec 2025 09:29:20 +0100 Subject: [PATCH] chore: throw FileNotFoundError for nonexistant files --- playwright/_impl/_set_input_files_helpers.py | 4 +++- tests/sync/test_locators.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/playwright/_impl/_set_input_files_helpers.py b/playwright/_impl/_set_input_files_helpers.py index 0f40d5b99..65307e0a2 100644 --- a/playwright/_impl/_set_input_files_helpers.py +++ b/playwright/_impl/_set_input_files_helpers.py @@ -14,6 +14,7 @@ import base64 import collections.abc import os +import stat from pathlib import Path from typing import ( TYPE_CHECKING, @@ -144,7 +145,8 @@ def resolve_paths_and_directory_for_input_files( local_paths: Optional[List[str]] = None local_directory: Optional[str] = None for item in items: - if os.path.isdir(item): + item_stat = os.stat(item) # Raises FileNotFoundError if doesn't exist + if stat.S_ISDIR(item_stat.st_mode): if local_directory: raise Error("Multiple directories are not supported") local_directory = str(Path(item).resolve()) diff --git a/tests/sync/test_locators.py b/tests/sync/test_locators.py index b554f0544..49d4d46ae 100644 --- a/tests/sync/test_locators.py +++ b/tests/sync/test_locators.py @@ -327,6 +327,12 @@ def test_locators_should_upload_a_file(page: Page, server: Server) -> None: ) +def test_locators_upload_nonexistant_file(page: Page, server: Server) -> None: + page.goto(server.PREFIX + "/input/fileupload.html") + with pytest.raises(FileNotFoundError): + page.locator("input[type=file]").set_input_files("nonexistant.html") + + def test_locators_should_press(page: Page) -> None: page.set_content("") page.locator("input").press("h")