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
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<testsuite name="Functional tests">
<directory>vendor/mink/driver-testsuite/tests</directory>
</testsuite>
<testsuite name="Regression tests">
<file>tests/Driver/AttachFileRelativePathTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
Expand Down
8 changes: 6 additions & 2 deletions src/Driver/PlaywrightDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,19 @@ 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');

if ('input' !== $tag || 'file' !== $type) {
throw new DriverException('attachFile: element is not a file input');
}

$element->setInputFiles([$path]);
$element->setInputFiles([$absolutePath]);
});
}

Expand Down
75 changes: 75 additions & 0 deletions tests/Driver/AttachFileRelativePathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Playwright\Mink\Tests\Driver;

use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Exception\DriverException;
use PHPUnit\Framework\TestCase;
use Playwright\Mink\Tests\Config;

/**
* Regression test for attachFile() resolving paths against the PHP process
* cwd instead of leaking a relative path to the Node bridge.
*
* @see https://github.com/playwright-php/playwright-mink/issues/ (attachFile relative path)
*/
final class AttachFileRelativePathTest extends TestCase
{
private ?DriverInterface $driver = null;
private ?string $originalCwd = null;

protected function tearDown(): void
{
$this->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,<input type="file" id="f">');

$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,<input type="file" id="f">');

$this->expectException(DriverException::class);

$this->driver->attachFile("//input[@id='f']", 'does-not-exist.txt');
}
}
Loading