MDL-40566 behat: Moving from behat_command to behat_selectors
authorDavid Monllao <davidm@moodle.com>
Tue, 6 Aug 2013 05:36:48 +0000 (13:36 +0800)
committerDavid Monllao <davidm@moodle.com>
Tue, 3 Sep 2013 02:28:42 +0000 (10:28 +0800)
admin/tool/behat/renderer.php
lib/behat/behat_base.php
lib/behat/classes/behat_command.php
lib/tests/behat/behat_hooks.php

index db94c85..0f5ac5e 100644 (file)
@@ -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
             );
index 02b968f..667273f 100644 (file)
@@ -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());
         }
 
index 67a3812..fb149ad 100644 (file)
@@ -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
index bf8ba91..6dbf386 100644 (file)
@@ -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();