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
10 changes: 10 additions & 0 deletions src/Api/Webpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public function value(string $selector): string
return $this->guessLocator($selector)->inputValue();
}

/**
* Get a locator for the given selector for use in custom assertions.
*
* When multiple elements match, use `->all()` to iterate or `->first()` / `->nth()` for one.
*/
public function element(string $selector, ?string $value = null): Locator
{
return $this->guessLocator($selector, $value);
}

/**
* Gets the locator for the given selector.
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Browser/Webpage/ElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

it('may assert text on all matching elements', function (): void {
Route::get('/', fn (): string => '
<a class="link">wow</a>
<a class="link">wow</a>
<a class="link">wow</a>
');

$page = visit('/');

foreach ($page->element('.link')->all() as $element) {
expect($element->textContent())->toBe('wow');
}
});

it('may assert text on the first matching element', function (): void {
Route::get('/', fn (): string => '
<a class="link">wow</a>
<a class="link">other</a>
');

$page = visit('/');

expect($page->element('.link')->first()->textContent())->toBe('wow');
});