From 9ae04968543da56657908eadbc74bcae900e9196 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 6 Aug 2013 13:36:48 +0800 Subject: [PATCH] MDL-40566 behat: Moving from behat_command to behat_selectors --- admin/tool/behat/renderer.php | 6 +++--- lib/behat/behat_base.php | 20 ++++++-------------- lib/behat/classes/behat_command.php | 28 ---------------------------- lib/tests/behat/behat_hooks.php | 6 ++++++ 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/admin/tool/behat/renderer.php b/admin/tool/behat/renderer.php index db94c85942b..0f5ac5e11dc 100644 --- a/admin/tool/behat/renderer.php +++ b/admin/tool/behat/renderer.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->libdir . '/behat/classes/behat_command.php'); +require_once($CFG->libdir . '/behat/classes/behat_selectors.php'); /** * Renderer for behat tool web features @@ -92,7 +92,7 @@ class tool_behat_renderer extends plugin_renderer_base { // Replace text selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/', function ($matches) { - return html_writer::select(behat_command::$allowedtextselectors, uniqid()); + return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid()); }, $stepsdefinitions ); @@ -100,7 +100,7 @@ class tool_behat_renderer extends plugin_renderer_base { // Replace selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/', function ($matches) { - return html_writer::select(behat_command::$allowedselectors, uniqid()); + return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid()); }, $stepsdefinitions ); diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index 02b968fec30..667273f6b89 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -367,22 +367,13 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { */ protected function transform_selector($selectortype, $element) { - // Here we don't know if a $allowedtextselector is used. - if (!isset(behat_command::$allowedselectors[$selectortype])) { + // Here we don't know if an allowed text selector is being used. + $selectors = behat_selectors::get_allowed_selectors(); + if (!isset($selectors[$selectortype])) { throw new ExpectationException('The "' . $selectortype . '" selector type does not exist', $this->getSession()); } - // CSS and XPath selectors locator is one single argument. - if ($selectortype == 'css_element' || $selectortype == 'xpath_element') { - $selector = str_replace('_element', '', $selectortype); - $locator = $element; - } else { - // Named selectors uses arrays as locators including the type of named selector. - $locator = array($selectortype, $this->getSession()->getSelectorsHandler()->xpathLiteral($element)); - $selector = 'named'; - } - - return array($selector, $locator); + return behat_selectors::get_behat_selector($selectortype, $element, $this->getSession()); } /** @@ -398,7 +389,8 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { */ protected function transform_text_selector($selectortype, $element) { - if ($selectortype != 'css_element' && $selectortype != 'xpath_element') { + $selectors = behat_selectors::get_allowed_text_selectors(); + if (empty($selectors[$selectortype])) { throw new ExpectationException('The "' . $selectortype . '" selector can not be used to select text nodes', $this->getSession()); } diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index 67a3812f533..fb149ad4ffa 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -42,34 +42,6 @@ class behat_command { */ const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing'; - /** - * @var Allowed types when using text selectors arguments. - */ - public static $allowedtextselectors = array( - 'css_element' => 'css_element', - 'xpath_element' => 'xpath_element' - ); - - /** - * @var Allowed types when using selector arguments. - */ - public static $allowedselectors = array( - 'link' => 'link', - 'button' => 'button', - 'link_or_button' => 'link_or_button', - 'select' => 'select', - 'checkbox' => 'checkbox', - 'radio' => 'radio', - 'file' => 'file', - 'optgroup' => 'optgroup', - 'option' => 'option', - 'table' => 'table', - 'field' => 'field', - 'fieldset' => 'fieldset', - 'css_element' => 'css_element', - 'xpath_element' => 'xpath_element' - ); - /** * Ensures the behat dir exists in moodledata * @return string Full path diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 645a647d90a..f807c86107e 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -89,6 +89,7 @@ class behat_hooks extends behat_base { // Now that we are MOODLE_INTERNAL. require_once(__DIR__ . '/../../behat/classes/behat_command.php'); + require_once(__DIR__ . '/../../behat/classes/behat_selectors.php'); require_once(__DIR__ . '/../../behat/classes/util.php'); require_once(__DIR__ . '/../../testing/classes/test_lock.php'); require_once(__DIR__ . '/../../testing/classes/nasty_strings.php'); @@ -138,6 +139,11 @@ class behat_hooks extends behat_base { throw new coding_exception('Behat only can modify the test database and the test dataroot!'); } + // We need the Mink session to do it and we do it only before the first scenario. + if (self::is_first_scenario()) { + behat_selectors::register_moodle_selectors($this->getSession()); + } + // Avoid some notices / warnings. $SESSION = new stdClass();