Permalink
Browse files

Merge branch 'MDL-38025_master' of git://github.com/dmonllao/moodle

  • Loading branch information...
2 parents 212e5ee + 46ac40c commit c0da001c4901c8386447b6506bbfdd1c82dd2686 @nebgor nebgor committed Mar 5, 2013
@@ -44,7 +44,7 @@ class tool_behat {
* @param string $type
* @param string $component
* @param string $filter
- * @return string
+ * @return array System steps or empty array if case there are no steps
*/
public static function stepsdefinitions($type, $component, $filter) {
@@ -70,15 +70,7 @@ public static function stepsdefinitions($type, $component, $filter) {
$options = ' --config="'.behat_config_manager::get_steps_list_config_filepath(). '" '.$filteroption;
list($steps, $code) = behat_command::run($options);
- if ($steps) {
- $stepshtml = implode('', $steps);
- }
-
- if (empty($stepshtml)) {
- $stepshtml = get_string('nostepsdefinitions', 'tool_behat');
- }
-
- return $stepshtml;
+ return $steps;
}
}
@@ -37,9 +37,9 @@
class tool_behat_renderer extends plugin_renderer_base {
/**
- * Renders the list of available steps according to the submitted filters
+ * Renders the list of available steps according to the submitted filters.
*
- * @param string $stepsdefinitions HTML from behat with the available steps
+ * @param mixed $stepsdefinitions Available steps array.
* @param moodleform $form
* @return string HTML code
*/
@@ -82,6 +82,30 @@ public function render_stepsdefinitions($stepsdefinitions, $form) {
$html .= ob_get_contents();
ob_end_clean();
+ if (empty($stepsdefinitions)) {
+ $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
+ } else {
+
+ $stepsdefinitions = implode('', $stepsdefinitions);
+
+ // Replace text selector type arguments with a user-friendly select.
+ $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR_STRING)/',
+ function ($matches) {
+ return html_writer::select(behat_command::$allowedtextselectors, uniqid());
+ },
+ $stepsdefinitions
+ );
+
+ // Replace selector type arguments with a user-friendly select.
+ $stepsdefinitions = preg_replace_callback('/(SELECTOR_STRING)/',
+ function ($matches) {
+ return html_writer::select(behat_command::$allowedselectors, uniqid());
+ },
+ $stepsdefinitions
+ );
+
+ }
+
// Steps definitions.
$html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
@@ -18,12 +18,12 @@ Feature: Page contents assertions
When I follow "Overview"
And I wait until the page is ready
And I wait "2" seconds
- And I hover ".region-content .generaltable td span"
+ And I hover ".region-content .generaltable td span" "css_element"
Then I should see "I'm the description"
And I should see "Filter groups by"
And I should not see "Filter groupssss by"
- And I should see "Group members" in the ".region-content table th.c1" element
- And I should not see "Group membersssss" in the ".region-content table th.c1" element
+ And I should see "Group members" in the ".region-content table th.c1" "css_element"
+ And I should not see "Group membersssss" in the ".region-content table th.c1" "css_element"
And I follow "Groups"
- And the element "#groupeditform #showcreateorphangroupform" should be enabled
- And the element "#groupeditform #showeditgroupsettingsform" should be disabled
+ And the "#groupeditform #showcreateorphangroupform" "css_element" should be enabled
+ And the "#groupeditform #showeditgroupsettingsform" "css_element" should be disabled
@@ -52,7 +52,6 @@ public static function merge_config($config, $localconfig) {
/**
* Allow access to protected method
* @see parent::get_config_file_contents()
- * @param string $prefix
* @param array $features
* @param array $stepsdefinitions
* @return string
View
@@ -59,18 +59,18 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
* @param string $path
* @return string
*/
- protected function locatePath($path) {
- $startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
- return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
+ protected function locate_path($path) {
+ $starturl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
+ return 0 !== strpos($path, 'http') ? $starturl . ltrim($path, '/') : $path;
}
/**
* Adapter to Behat\Mink\Element\Element::find() using the spin() method.
*
* @link http://mink.behat.org/#traverse-the-page-selectors
- * @param Exception $exception Otherwise we throw expcetion with generic info
* @param string $selector The selector type (css, xpath, named...)
* @param mixed $locator It depends on the $selector, can be the xpath, a name, a css locator...
+ * @param Exception $exception Otherwise we throw exception with generic info
* @return NodeElement
*/
protected function find($selector, $locator, $exception = false) {
@@ -98,7 +98,7 @@ function($context, $args) {
array($selector, $locator),
self::TIMEOUT,
$exception
- );
+ );
}
/**
@@ -119,7 +119,7 @@ function($context, $args) {
*
* @link http://mink.behat.org/#named-selectors
* @throws coding_exception
- * @param string $method The name of the called method
+ * @param string $name The name of the called method
* @param mixed $arguments
* @return NodeElement
*/
@@ -193,7 +193,7 @@ protected function spin($lambda, $args = false, $timeout = false, $exception = f
if ($return = $lambda($this, $args)) {
return $return;
}
- } catch(Exception $e) {
+ } catch (Exception $e) {
// We would use the first closure exception if no exception has been provided.
if (!$exception) {
@@ -216,4 +216,56 @@ protected function spin($lambda, $args = false, $timeout = false, $exception = f
throw $exception;
}
+ /**
+ * Gets a NodeElement based on the locator and selector type received as argument from steps definitions.
+ *
+ * @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $selectortype
+ * @param string $element
+ * @return NodeElement
+ */
+ protected function get_selected_node($selectortype, $element) {
+
+ // Getting Mink selector and locator.
+ list($selector, $locator) = $this->transform_selector($selectortype, $element);
+
+ // Returns the NodeElement.
+ return $this->find($selector, $locator);
+ }
+
+ /**
+ * Transforms from step definition's argument style to Mink format.
+ *
+ * Mink has 3 different selectors css, xpath and named, where named
+ * selectors includes link, button, field... to simplify and group multiple
+ * steps in one we use the same interface, considering all link, buttons...
+ * at the same level as css selectors and xpath; this method makes the
+ * conversion from the arguments received by the steps to the selectors and locators
+ * required to interact with Mink.
+ *
+ * @throws ExpectationException
+ * @param string $selectortype It can be css, xpath or any of the named selectors.
+ * @param string $element The locator (or string) we are looking for.
+ * @return array Contains the selector and the locator expected by Mink.
+ */
+ protected function transform_selector($selectortype, $element) {
+
+ // Here we don't know if a $allowedtextselector is used.
+ if (!isset(behat_command::$allowedselectors[$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);
+ }
+
}
@@ -42,6 +42,34 @@ 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
@@ -1,5 +1,4 @@
<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -1,5 +1,4 @@
<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -41,7 +40,14 @@
*/
class behat_form_field {
+ /**
+ * @var Session Behat session.
+ */
protected $session;
+
+ /**
+ * @var NodeElement The field DOM node to interact with.
+ */
protected $fieldnode;
/**
@@ -1,5 +1,4 @@
<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -51,6 +50,9 @@
*/
class behat_data_generators extends behat_base {
+ /**
+ * @var testing_data_generator
+ */
protected $datagenerator;
/**
@@ -215,7 +217,7 @@ protected function adapt_enrol_user($data) {
/**
* Gets the user id from it's username.
* @throws Exception
- * @param string $idnumber
+ * @param string $username
* @return int
*/
protected function get_user_id($username) {
@@ -230,7 +232,7 @@ protected function get_user_id($username) {
/**
* Gets the role id from it's shortname.
* @throws Exception
- * @param string $idnumber
+ * @param string $roleshortname
* @return int
*/
protected function get_role_id($roleshortname) {
@@ -50,6 +50,7 @@ class behat_forms extends behat_base {
*
* @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $button
*/
public function press_button($button) {
@@ -90,6 +91,8 @@ public function i_fill_the_moodle_form_with(TableNode $data) {
*
* @When /^I fill in "(?P<field_string>(?:[^"]|\\")*)" with "(?P<value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $field
+ * @param string $value
*/
public function fill_field($field, $value) {
@@ -102,6 +105,8 @@ public function fill_field($field, $value) {
*
* @When /^I select "(?P<option_string>(?:[^"]|\\")*)" from "(?P<select_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $option
+ * @param string $select
*/
public function select_option($option, $select) {
@@ -117,6 +122,7 @@ public function select_option($option, $select) {
*
* @When /^I check "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $option
*/
public function check_option($option) {
@@ -129,6 +135,7 @@ public function check_option($option) {
*
* @When /^I uncheck "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
+ * @param string $option
*/
public function uncheck_option($option) {
@@ -142,8 +149,8 @@ public function uncheck_option($option) {
* @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
- * @param mixed $locator
- * @param mixed $value
+ * @param string $locator
+ * @param string $value
*/
public function the_field_should_match_value($locator, $value) {
@@ -166,6 +173,7 @@ public function the_field_should_match_value($locator, $value) {
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should be checked$/
* @see Behat\MinkExtension\Context\MinkContext
+ * @param string $checkbox
*/
public function assert_checkbox_checked($checkbox) {
$this->assertSession()->checkboxChecked($checkbox);
@@ -176,6 +184,7 @@ public function assert_checkbox_checked($checkbox) {
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should not be checked$/
* @see Behat\MinkExtension\Context\MinkContext
+ * @param string $checkbox
*/
public function assert_checkbox_not_checked($checkbox) {
$this->assertSession()->checkboxNotChecked($checkbox);
Oops, something went wrong.

0 comments on commit c0da001

Please sign in to comment.