Last active
October 12, 2022 20:56
Laravel Dusk find by text and selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Laravel\Dusk\Browser; | |
use Laravel\Dusk\ElementResolver; | |
// add macros on the boot method of a service provider i.e. DuskServiceProvider | |
ElementResolver::macro("findBySelectorAndText",function ($selector,$text) | |
{ | |
$elements=[]; | |
// imitates ElementResolver::findButtonByText | |
foreach ($this->all($selector) as $element) { | |
if (Str::contains($element->getText(), $text)) { | |
$elements[]=$element; | |
} | |
} | |
return $elements; | |
}); | |
// imitates Concerns\InteractsWithMouse::click | |
Browser::macro("clickElementWithText",function ($selector,$text) | |
{ | |
$elements=$this->resolver->findBySelectorAndText($selector,$text); | |
foreach ($elements as $one) { | |
$one->click(); | |
} | |
return $this; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment