From 6176c8fc88b05cbac3f276a48463ab964a268c4e Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 18 Nov 2022 15:37:59 +0000 Subject: [PATCH] MDL-76399 behat: improvements to "should exist in" thrown exception. --- lib/behat/classes/behat_session_trait.php | 3 +-- lib/tests/behat/behat_general.php | 21 +++++++-------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index a9c5f317f89..3e38c3c08c1 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -440,13 +440,12 @@ trait behat_session_trait { if ($containerselectortype === 'NodeElement' && is_a($containerelement, NodeElement::class)) { // Support a NodeElement being passed in for use in step chaining. $containernode = $containerelement; - $locatorexceptionmsg = $element; } else { // Gets the container, it will always be text based. $containernode = $this->get_text_selector_node($containerselectortype, $containerelement); - $locatorexceptionmsg = $element . '" in the "' . $containerelement. '" "' . $containerselectortype. '"'; } + $locatorexceptionmsg = $element . '" in the "' . $this->get_selector_description($containerselectortype, $containerelement); $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg); return $this->find($selectortype, $element, $exception, $containernode); diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 6979764536e..ecdb68e8b48 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -28,6 +28,7 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); use Behat\Gherkin\Node\TableNode; +use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\ElementNotFoundException; use Behat\Mink\Exception\ExpectationException; @@ -1217,20 +1218,12 @@ EOF; * @throws ElementNotFoundException Thrown by behat_base::find * @param string $element The locator of the specified selector * @param string $selectortype The selector type - * @param string $containerelement The container selector type - * @param string $containerselectortype The container locator + * @param NodeElement|string $containerelement The locator of the container selector + * @param string $containerselectortype The container selector type */ public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) { - // Get the container node. - $containernode = $this->find($containerselectortype, $containerelement); - - // Specific exception giving info about where can't we find the element. - $containerdescription = $this->get_selector_description($containerselectortype, $containerelement); - $locatorexceptionmsg = "{$element} not found in the {$containerdescription}}"; - $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg); - - // Looks for the requested node inside the container node. - $this->find($selectortype, $element, $exception, $containernode); + // Will throw an ElementNotFoundException if it does not exist. + $this->get_node_in_container($selectortype, $element, $containerselectortype, $containerelement); } /** @@ -1242,8 +1235,8 @@ EOF; * @throws ExpectationException * @param string $element The locator of the specified selector * @param string $selectortype The selector type - * @param string $containerelement The container selector type - * @param string $containerselectortype The container locator + * @param NodeElement|string $containerelement The locator of the container selector + * @param string $containerselectortype The container selector type */ public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) { // Get the container node.