From e5b1fc80834eb7463a21cab9c3b6bc6c71e0eadf Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 12 Nov 2020 08:59:51 +0800 Subject: [PATCH] MDL-66979 behat: Core updates for W3C WebDriver This commit updates core Behat features to make use of the php-webdriver/webdriver library instead of the legacy Instaclick library. Most of these changes relate to use of features of WebDriver which we are required to use directly rather than through the Mink Driver. --- lib/behat/classes/behat_session_trait.php | 18 ++++++++---------- lib/behat/form_field/behat_form_field.php | 7 +++++-- lib/tests/behat/behat_general.php | 21 ++++++++++----------- lib/tests/behat/behat_hooks.php | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index 5e9d3c0d35d..792c6872ae4 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -23,11 +23,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use Behat\Mink\Element\NodeElement; +use Behat\Mink\Element\Element; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Exception\ElementNotFoundException; -use Behat\Mink\Element\NodeElement; -use Behat\Mink\Element\Element; +use Behat\Mink\Exception\NoSuchWindowException; use Behat\Mink\Session; // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. @@ -35,9 +36,8 @@ use Behat\Mink\Session; require_once(__DIR__ . '/component_named_replacement.php'); require_once(__DIR__ . '/component_named_selector.php'); -// Alias the WebDriver\Key class to behat_keys to make future transition to a different WebDriver implementation -// easier. -class_alias('WebDriver\\Key', 'behat_keys'); +// Alias the Facebook\WebDriver\WebDriverKeys class to behat_keys for better b/c with the older Instaclick driver. +class_alias('Facebook\WebDriver\WebDriverKeys', 'behat_keys'); /** * A trait containing functionality used by the behat base context, and form fields. @@ -253,9 +253,7 @@ trait behat_session_trait { * @param string[] $keys */ public static function type_keys(Session $session, array $keys): void { - $session->getDriver()->getWebDriverSession()->keys([ - 'value' => $keys, - ]); + $session->getDriver()->getWebDriver()->getKeyboard()->sendKeys($keys); } /** @@ -833,7 +831,7 @@ EOF; return M.util.pending_js.join(":"); })()')); $pending = self::evaluate_script_in_session($session, $jscode); - } catch (NoSuchWindow $nsw) { + } catch (NoSuchWindowException $nsw) { // We catch an exception here, in case we just closed the window we were interacting with. // No javascript is running if there is no window right? $pending = ''; @@ -973,7 +971,7 @@ EOF; } } - } catch (NoSuchWindow $e) { + } catch (NoSuchWindowException $e) { // If we were interacting with a popup window it will not exists after closing it. } catch (DriverException $e) { // Same reason as above. diff --git a/lib/behat/form_field/behat_form_field.php b/lib/behat/form_field/behat_form_field.php index c2aeee7559f..4da8002404a 100644 --- a/lib/behat/form_field/behat_form_field.php +++ b/lib/behat/form_field/behat_form_field.php @@ -218,12 +218,15 @@ class behat_form_field implements behat_session_interface { * @return int */ protected function get_internal_field_id() { - if (!$this->running_javascript()) { throw new coding_exception('You can only get an internal ID using the selenium driver.'); } - return $this->session->getDriver()->getWebDriverSession()->element('xpath', $this->field->getXPath())->getID(); + return $this->getSession() + ->getDriver() + ->getWebDriver() + ->findElement(WebDriverBy::xpath($node->getXpath())) + ->getID(); } /** diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index ec1e3a42984..70f9aa91532 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -271,7 +271,7 @@ class behat_general extends behat_base { * @Given /^I accept the currently displayed dialog$/ */ public function accept_currently_displayed_alert_dialog() { - $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); + $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->accept(); } /** @@ -279,7 +279,7 @@ class behat_general extends behat_base { * @Given /^I dismiss the currently displayed dialog$/ */ public function dismiss_currently_displayed_alert_dialog() { - $this->getSession()->getDriver()->getWebDriverSession()->dismiss_alert(); + $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->dismiss(); } /** @@ -418,7 +418,8 @@ class behat_general extends behat_base { */ public function i_click_on_confirming_the_dialogue($element, $selectortype) { $this->i_click_on($element, $selectortype); - $this->accept_currently_displayed_alert_dialog(); + $this->execute('behat_general::accept_currently_displayed_alert_dialog', []); + $this->wait_until_the_page_is_ready(); } /** @@ -431,7 +432,8 @@ class behat_general extends behat_base { */ public function i_click_on_dismissing_the_dialogue($element, $selectortype) { $this->i_click_on($element, $selectortype); - $this->dismiss_currently_displayed_alert_dialog(); + $this->execute('behat_general::dismiss_currently_displayed_alert_dialog', []); + $this->wait_until_the_page_is_ready(); } /** @@ -1802,16 +1804,16 @@ EOF; $modifier = trim($key); switch (strtoupper($key)) { case 'UP': - $keys[] = behat_keys::UP_ARROW; + $keys[] = behat_keys::ARROW_UP; break; case 'DOWN': - $keys[] = behat_keys::DOWN_ARROW; + $keys[] = behat_keys::ARROW_DOWN; break; case 'LEFT': - $keys[] = behat_keys::LEFT_ARROW; + $keys[] = behat_keys::ARROW_LEFT; break; case 'RIGHT': - $keys[] = behat_keys::RIGHT_ARROW; + $keys[] = behat_keys::ARROW_RIGHT; break; case 'HOME': $keys[] = behat_keys::HOME; @@ -1852,9 +1854,6 @@ EOF; throw new \coding_exception("Unknown key '$key'}"); } - // Always send the NULL key as the last key. - $keys[] = behat_keys::NULL_KEY; - behat_base::type_keys($this->getSession(), $keys); } diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 17b8a0ed1a4..d7fc9e77287 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -644,7 +644,7 @@ EOF; // the following scenarios. Some browsers already closes the alert, so // wrapping in a try & catch. try { - $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); + $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->accept(); } catch (Exception $e) { // Catching the generic one as we never know how drivers reacts here. }