From 35f15120fb06317243ab7653c4d6f044f1bdfe26 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 3 Mar 2025 17:25:10 +1300 Subject: [PATCH 1/2] ENH Use generic JavaScript for selecting HTML content --- src/Context/FixtureContext.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Context/FixtureContext.php b/src/Context/FixtureContext.php index 40ffaa0d..8381fd5a 100644 --- a/src/Context/FixtureContext.php +++ b/src/Context/FixtureContext.php @@ -989,20 +989,8 @@ protected function selectInTheHtmlField(string $select, string $field) $inputField->getParent()->find('css', 'iframe')->click(); $inputFieldId = $inputField->getAttribute('id'); $js = <<getMainContext()->getSession()->executeScript($js); } From aef2e1c440ea21daee9309e5f0f88cd0b3e21a11 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 6 Mar 2025 16:28:05 +1300 Subject: [PATCH 2/2] API Allow other contexts to reuse findNamedButton method Also adds an optional param for the parent, in case you want to find a button inside a specific element. --- src/Context/BasicContext.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index f57ae454..a14f7b6b 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -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; @@ -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()) {