From 4db499a8eb23a1bfaa9f550d1475bfe89fbb97bc Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 22 Nov 2024 10:14:54 +1300 Subject: [PATCH] ENH Allow clicking on text directly in an element This resolves situations where the text in a child element gets confused with text in the element we want to click and results in errors finding the correct element. --- src/Context/BasicContext.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index c335f376..f6128fa3 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -496,12 +496,12 @@ public function iClickOnTheElementConfirmingTheDialog($selector) } /** - * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/ + * @Given /^I (click|double click) "([^"]*)"(| directly) in the "([^"]*)" element$/ * @param string $clickType * @param string $text * @param string $selector */ - public function iClickInTheElement($clickType, $text, $selector) + public function iClickInTheElement($clickType, $text, $directly, $selector) { $clickTypeMap = array( "double click" => "doubleclick", @@ -510,7 +510,13 @@ public function iClickInTheElement($clickType, $text, $selector) $page = $this->getSession()->getPage(); $parentElement = $page->find('css', $selector); Assert::assertNotNull($parentElement, sprintf('"%s" element not found', $selector)); - $element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text)); + if ($directly) { + // Finds the specific text within the selector element (to validate it's there), and then grabs the element that holds the text + $element = $parentElement->find('xpath', sprintf('/text()[contains(.,"%s")]/..', $text)); + } else { + // Finds a child of the selector element which contains the text + $element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text)); + } Assert::assertNotNull($element, sprintf('"%s" not found', $text)); $clickTypeFn = $clickTypeMap[$clickType]; $element->$clickTypeFn();