Skip to content

Commit

Permalink
Merge branch 'MDL-40566_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 3, 2013
2 parents f82a082 + fcc0b5b commit 58c80b0
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 73 deletions.
6 changes: 3 additions & 3 deletions admin/tool/behat/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,15 +92,15 @@ public function render_stepsdefinitions($stepsdefinitions, $form) {
// 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
);

// 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
);
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/behat/tests/behat/basic_actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Feature: Page contents assertions
| Course 1 | C1 | 0 |
And I log in as "admin"
And I follow "Course 1"
When I click on "Move this to the dock" "button" in the ".block_settings" "css_element"
When I click on "Move this to the dock" "button" in the "Administration" "block"
Then I should not see "Question bank"
And I click on "//div[@id='dock']/descendant::h2[normalize-space(.)='Administration']" "xpath_element"

Expand All @@ -45,5 +45,5 @@ Feature: Page contents assertions
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
When I click on "Move this to the dock" "button" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' block_settings ')]" "xpath_element"
When I click on "Move this to the dock" "button" in the "Administration" "block"
Then I should not see "Turn editing on"
7 changes: 4 additions & 3 deletions backup/util/ui/tests/behat/backup_courses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Feature: Backup Moodle courses
@javascript
Scenario: Backup a course providing options
When I backup "Course 1" course using this options:
| Filename | test_backup.mbz |
Then I should see "Restore"
And I click on "Restore" "link" in the ".backup-files-table" "css_element"
And I click on "Restore" "link" in the "test_backup.mbz" "table_row"
And I should see "URL of backup"
And I should see "Anonymize user information"

Expand All @@ -27,11 +28,11 @@ Feature: Backup Moodle courses
| setting_section_section_5_userinfo | 0 |
| setting_section_section_5_included | 0 |
Then I should see "Restore"
And I click on "Restore" "link" in the ".backup-files-table" "css_element"
And I click on "Restore" "link" in the "test_backup.mbz" "table_row"
And I should not see "Section 3"
And I press "Continue"
And I click on "Continue" "button" in the ".bcs-current-course" "css_element"
And "//div[contains(concat(' ', normalize-space(@class), ' '), ' fitem ')][contains(., 'Include calendar events')]/descendant::img" "xpath_element" should exists
And I check "Include course logs"
And I press "Cancel"
And I click on "Cancel" "button" in the ".confirmation-dialogue" "css_element"
And I click on "Cancel" "button" in the "Cancel backup" "dialogue"
34 changes: 17 additions & 17 deletions blocks/navigation/tests/behat/view_my_courses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ Feature: View my courses in navigation block
And I log out
And I log in as "student1"
When I follow "My home"
Then I should not see "cat1" in the "div.block_navigation .type_system" "css_element"
And I should not see "cat2" in the "div.block_navigation .type_system" "css_element"
And I should see "c1" in the "div.block_navigation .type_system" "css_element"
And I should see "c31" in the "div.block_navigation .type_system" "css_element"
And I should see "c331" in the "div.block_navigation .type_system" "css_element"
And I should not see "c2" in the "div.block_navigation .type_system" "css_element"
And I should not see "c32" in the "div.block_navigation .type_system" "css_element"
And I should not see "c332" in the "div.block_navigation .type_system" "css_element"
Then I should not see "cat1" in the "Navigation" "block"
And I should not see "cat2" in the "Navigation" "block"
And I should see "c1" in the "Navigation" "block"
And I should see "c31" in the "Navigation" "block"
And I should see "c331" in the "Navigation" "block"
And I should not see "c2" in the "Navigation" "block"
And I should not see "c32" in the "Navigation" "block"
And I should not see "c332" in the "Navigation" "block"

@javascript
Scenario: The nested list of enrolled courses is shown
Expand All @@ -54,18 +54,18 @@ Feature: View my courses in navigation block
And I log out
And I log in as "student1"
When I follow "My home"
Then I should see "cat1" in the "div.block_navigation .type_system" "css_element"
And I should see "cat3" in the "div.block_navigation .type_system" "css_element"
And I should not see "cat2" in the "div.block_navigation .type_system" "css_element"
Then I should see "cat1" in the "Navigation" "block"
And I should see "cat3" in the "Navigation" "block"
And I should not see "cat2" in the "Navigation" "block"
And I expand "cat3" node
And I wait "2" seconds
And I should see "cat31" in the "div.block_navigation .type_system" "css_element"
And I should see "cat33" in the "div.block_navigation .type_system" "css_element"
And I should not see "cat32" in the "div.block_navigation .type_system" "css_element"
And I should see "cat31" in the "Navigation" "block"
And I should see "cat33" in the "Navigation" "block"
And I should not see "cat32" in the "Navigation" "block"
And I expand "cat31" node
And I wait "2" seconds
And I should see "c31" in the "div.block_navigation .type_system" "css_element"
And I should see "c31" in the "Navigation" "block"
And I expand "cat33" node
And I wait "2" seconds
And I should see "c331" in the "div.block_navigation .type_system" "css_element"
And I should not see "c332" in the "div.block_navigation .type_system" "css_element"
And I should see "c331" in the "Navigation" "block"
And I should not see "c332" in the "Navigation" "block"
12 changes: 6 additions & 6 deletions course/tests/behat/course_controls.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Feature: Course activity controls works as expected
And I should see "Turn editing on"
And "Turn editing on" "button" should exists
And I turn editing mode on
And I click on "Actions" "link" in the ".block_recent_activity" "css_element"
And I click on "Actions" "link" in the "Recent activity" "block"
And I click on "Delete Recent activity block" "link"
And I press "Yes"
And "#section-2" "css_element" <should_see_other_sections> exists
Expand Down Expand Up @@ -77,8 +77,8 @@ Feature: Course activity controls works as expected
And section "1" should be visible
And I add the "Section links" block
And "#section-2" "css_element" <should_see_other_sections> exists
And I should see "1 2 3 4 5" in the ".block_section_links" "css_element"
And I click on "2" "link" in the ".block_section_links" "css_element"
And I should see "1 2 3 4 5" in the "Section links" "block"
And I click on "2" "link" in the "Section links" "block"
And I <should_see_other_sections_following_block_sections_links> see "Test forum name 2"

Examples:
Expand Down Expand Up @@ -114,7 +114,7 @@ Feature: Course activity controls works as expected
And I should see "Turn editing on"
And "Turn editing on" "button" should exists
And I turn editing mode on
And I click on "Actions" "link" in the ".block_recent_activity" "css_element"
And I click on "Actions" "link" in the "Recent activity" "block"
And I click on "Delete Recent activity block" "link"
And I press "Yes"
And "#section-2" "css_element" <should_see_other_sections> exists
Expand Down Expand Up @@ -154,8 +154,8 @@ Feature: Course activity controls works as expected
And section "1" should be visible
And I add the "Section links" block
And "#section-2" "css_element" <should_see_other_sections> exists
And I should see "1 2 3 4 5" in the ".block_section_links" "css_element"
And I click on "2" "link" in the ".block_section_links" "css_element"
And I should see "1 2 3 4 5" in the "Section links" "block"
And I click on "2" "link" in the "Section links" "block"
And I <should_see_other_sections_following_block_sections_links> see "Test forum name 2"

Examples:
Expand Down
20 changes: 6 additions & 14 deletions lib/behat/behat_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,13 @@ protected function get_node_in_container($selectortype, $element, $containersele
*/
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());
}

/**
Expand All @@ -398,7 +389,8 @@ protected function transform_selector($selectortype, $element) {
*/
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());
}

Expand Down
28 changes: 0 additions & 28 deletions lib/behat/classes/behat_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
161 changes: 161 additions & 0 deletions lib/behat/classes/behat_selectors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Moodle-specific selectors.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Moodle selectors manager.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_selectors {

/**
* @var Allowed types when using text selectors arguments.
*/
protected static $allowedtextselectors = array(
'dialogue' => 'dialogue',
'block' => 'block',
'region' => 'region',
'table_row' => 'table_row',
'table' => 'table',
'fieldset' => 'fieldset',
'css_element' => 'css_element',
'xpath_element' => 'xpath_element'
);

/**
* @var Allowed types when using selector arguments.
*/
protected static $allowedselectors = array(
'dialogue' => 'dialogue',
'block' => 'block',
'region' => 'region',
'table_row' => 'table_row',
'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'
);

/**
* Behat by default comes with XPath, CSS and named selectors,
* named selectors are a mapping between names (like button) and
* xpaths that represents that names and includes a placeholder that
* will be replaced by the locator. These are Moodle's own xpaths.
*
* @var XPaths for moodle elements.
*/
protected static $moodleselectors = array(
'dialogue' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]/descendant::h1[normalize-space(.) = %locator%]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]
XPATH
, 'block' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' '))] | .//div[contains(concat(' ', normalize-space(@class), ' '), ' block ')]/descendant::h2[normalize-space(.) = %locator%]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' block ')]
XPATH
, 'region' => <<<XPATH
.//div[./@id = %locator%]
XPATH
, 'table_row' => <<<XPATH
.//tr[contains(normalize-space(.), %locator%)]
XPATH
);

/**
* Returns the behat selector and locator for a given moodle selector and locator
*
* @param string $selectortype The moodle selector type, which includes moodle selectors
* @param string $element The locator we look for in that kind of selector
* @param Session $session The Mink opened session
* @return array Contains the selector and the locator expected by Mink.
*/
public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {

// 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, $session->getSelectorsHandler()->xpathLiteral($element));
$selector = 'named';
}

return array($selector, $locator);
}

/**
* Adds moodle selectors as behat named selectors.
*
* @param Session $session The mink session
* @return void
*/
public static function register_moodle_selectors(Behat\Mink\Session $session) {

foreach (self::get_moodle_selectors() as $name => $xpath) {
$session->getSelectorsHandler()->getSelector('named')->registerNamedXpath($name, $xpath);
}
}

/**
* Allowed selectors getter.
*
* @return array
*/
public static function get_allowed_selectors() {
return self::$allowedselectors;
}

/**
* Allowed text selectors getter.
*
* @return array
*/
public static function get_allowed_text_selectors() {
return self::$allowedtextselectors;
}

/**
* Moodle selectors attribute accessor.
*
* @return array
*/
protected static function get_moodle_selectors() {
return self::$moodleselectors;
}
}
Loading

0 comments on commit 58c80b0

Please sign in to comment.