Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/Api/Concerns/InteractsWithElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ public function uncheck(string $field, ?string $value = null): self
}

/**
* Attach the given file to the field.
* Attach the given files to the field.
*
* @param string|array<string> $path
*/
public function attach(string $field, string $path): self
public function attach(string $field, string|array $path): self
{
$this->guessLocator($field)->setInputFiles($path);

Expand Down
6 changes: 4 additions & 2 deletions src/Playwright/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,12 @@ public function tap(?array $options = null): void

/**
* Set input files for a file input element.
*
* @param string|array<string> $path
*/
public function setInputFiles(string $path): void
public function setInputFiles(string|array $path): void
{
$params = ['localPaths' => [$path]];
$params = ['localPaths' => is_array($path) ? $path : [$path]];
$response = $this->sendMessage('setInputFiles', $params);

$this->processVoidResponse($response);
Expand Down
30 changes: 30 additions & 0 deletions tests/Browser/Webpage/AttachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,33 @@
// Clean up
unlink($tempFile);
});

it('may attach multiple files to a file input', function (): void {
Route::get('/', fn (): string => '
<form>
<input type="file" id="avatar" name="avatar" multiple>
</form>
');

$page = visit('/');

// Create temporary files
$tempFiles = [
tempnam(sys_get_temp_dir(), 'test'),
tempnam(sys_get_temp_dir(), 'test'),
];
file_put_contents($tempFiles[0], 'test content 1');
file_put_contents($tempFiles[1], 'test content 2');

$page->attach('avatar', $tempFiles);

// Check that the files are attached
$fileNames = array_map(fn ($file) => basename($file), $tempFiles);
expect($page->script('() => document.querySelector("input[name=avatar]").files.length'))->toBe(count($tempFiles));
expect($page->script('() => document.querySelector("input[name=avatar]").files.map(file => file.name)'))->toBe($fileNames);

// Clean up
foreach ($tempFiles as $tempFile) {
unlink($tempFile);
}
});