From 6a73a888065a9b37a1fcc265ad0f27762261c97e Mon Sep 17 00:00:00 2001 From: Roman Kukla Date: Tue, 7 Jul 2026 23:57:07 +0300 Subject: [PATCH] Fix attachFile() forwarding relative paths to the Node bridge --- phpunit.xml.dist | 3 + src/Driver/PlaywrightDriver.php | 8 ++- tests/Driver/AttachFileRelativePathTest.php | 75 +++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 tests/Driver/AttachFileRelativePathTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b4bfda1..acc777a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,6 +7,9 @@ vendor/mink/driver-testsuite/tests + + tests/Driver/AttachFileRelativePathTest.php + diff --git a/src/Driver/PlaywrightDriver.php b/src/Driver/PlaywrightDriver.php index a46def1..0f2f0b6 100644 --- a/src/Driver/PlaywrightDriver.php +++ b/src/Driver/PlaywrightDriver.php @@ -613,7 +613,11 @@ public function rightClick(string $xpath): void public function attachFile(string $xpath, string $path): void { $element = $this->first($xpath); - $this->safe(function () use ($element, $path) { + $absolutePath = realpath($path); + if (false === $absolutePath) { + throw new DriverException(sprintf('File "%s" does not exist', $path)); + } + $this->safe(function () use ($element, $absolutePath) { $tag = $element->evaluate('el => el.tagName.toLowerCase()'); $type = $element->getAttribute('type'); @@ -621,7 +625,7 @@ public function attachFile(string $xpath, string $path): void throw new DriverException('attachFile: element is not a file input'); } - $element->setInputFiles([$path]); + $element->setInputFiles([$absolutePath]); }); } diff --git a/tests/Driver/AttachFileRelativePathTest.php b/tests/Driver/AttachFileRelativePathTest.php new file mode 100644 index 0000000..d0363d2 --- /dev/null +++ b/tests/Driver/AttachFileRelativePathTest.php @@ -0,0 +1,75 @@ +driver?->stop(); + + if (null !== $this->originalCwd) { + chdir($this->originalCwd); + } + } + + public function testAttachFileAcceptsPathRelativeToPhpCwd(): void + { + $tmpDir = sys_get_temp_dir().'/playwright-mink-attach-file-test'; + if (!is_dir($tmpDir)) { + mkdir($tmpDir); + } + file_put_contents($tmpDir.'/upload.txt', 'hello'); + + $this->originalCwd = getcwd() ?: null; + chdir($tmpDir); + + $this->driver = Config::getInstance()->createDriver(); + $this->driver->start(); + $this->driver->visit('data:text/html,'); + + $this->driver->attachFile("//input[@id='f']", 'upload.txt'); + + self::assertSame( + 'upload.txt', + $this->driver->evaluateScript("document.getElementById('f').files[0].name") + ); + } + + public function testAttachFileThrowsAClearErrorForAMissingRelativePath(): void + { + $this->driver = Config::getInstance()->createDriver(); + $this->driver->start(); + $this->driver->visit('data:text/html,'); + + $this->expectException(DriverException::class); + + $this->driver->attachFile("//input[@id='f']", 'does-not-exist.txt'); + } +}