Skip to content

Commit

Permalink
API Allow other contexts to reuse findNamedButton method
Browse files Browse the repository at this point in the history
Also adds an optional param for the parent, in case you want to find a
button inside a specific element.
  • Loading branch information
GuySartorelli committed Mar 6, 2025
1 parent 35f1512 commit aef2e1c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Element\ElementInterface;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Session;
Expand Down Expand Up @@ -375,18 +376,19 @@ public function stepIWaitFor($secs)
* @param string $title
* @return NodeElement|null
*/
protected function findNamedButton($title)
public function findNamedButton($title, ?ElementInterface $parent = null)
{
$page = $this->getSession()->getPage();
if ($parent === null) {
$parent = $this->getSession()->getPage();
}
// See https://mathiasbynens.be/notes/css-escapes
$escapedTitle = addcslashes($title ?? '', '!"#$%&\'()*+,-./:;<=>?@[\]^`{|}~');
$matchedEl = null;
$searches = [
['named', ['link_or_button', "'{$title}'"]],
['css', "button[data-text-alternate='{$escapedTitle}']"],
];
foreach ($searches as list($type, $arg)) {
$buttons = $page->findAll($type, $arg);
$buttons = $parent->findAll($type, $arg);
/** @var NodeElement $button */
foreach ($buttons as $button) {
if ($button->isVisible()) {
Expand Down

0 comments on commit aef2e1c

Please sign in to comment.