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');
+ }
+}