MDL-44619 behat: Converting MDLQA-8

In a standard forum, students can start discussions
add replies and attach files to their posts.
This commit is contained in:
David Monllao 2014-04-02 17:18:46 +08:00
parent 9bc60e016b
commit af4830a257
11 changed files with 133 additions and 29 deletions

View File

@ -349,9 +349,7 @@ class behat_backup extends behat_base {
foreach ($datahash as $locator => $value) {
try {
// Using $this->find* to enforce stability over speed.
$fieldnode = $this->find_field($locator);
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);
$field->set_value($value);
} catch (ElementNotFoundException $e) {

View File

@ -26,7 +26,9 @@
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
use Behat\Mink\Session as Session,
Behat\Mink\Element\NodeElement as NodeElement;
Behat\Mink\Element\NodeElement as NodeElement,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\MinkExtension\Context\RawMinkContext as RawMinkContext;
/**
* Helper to interact with form fields.
@ -38,6 +40,30 @@ use Behat\Mink\Session as Session,
*/
class behat_field_manager {
/**
* Gets an instance of the form field from it's label
*
* @param string $label
* @param RawMinkContext $context
* @return behat_form_field
*/
public static function get_form_field_from_label($label, RawMinkContext $context) {
// There are moodle form elements that are not directly related with
// a basic HTML form field, we should also take care of them.
try {
// The DOM node.
$fieldnode = $context->find_field($label);
} catch (ElementNotFoundException $e) {
// Looking for labels that points to filemanagers.
$fieldnode = $context->find_filemanager($label);
}
// The behat field manager.
return self::get_form_field($fieldnode, $context->getSession());
}
/**
* Gets an instance of the form field.
*
@ -217,6 +243,7 @@ class behat_field_manager {
* @todo MDL-XXXXX This will be deleted in Moodle 2.8
* @see behat_field_manager::get_form_field()
* @param NodeElement $fieldnode
* @param string $locator
* @param Session $session The behat browser session
* @return behat_form_field
*/
@ -237,6 +264,7 @@ class behat_field_manager {
* @todo MDL-XXXXX This will be deleted in Moodle 2.8
* @see behat_field_manager::get_field_node_type()
* @param NodeElement $fieldnode The current node.
* @param string $locator
* @param Session $session The behat browser session
* @return mixed A NodeElement if we continue looking for the element type and String or false when we are done.
*/

View File

@ -18,7 +18,7 @@
* Helper to initialise behat contexts from moodle code.
*
* @package core
* @category testing
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -32,7 +32,7 @@ use Behat\Mink\Session as Session,
* Helper to get behat contexts.
*
* @package core
* @category testing
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -71,7 +71,7 @@ class behat_context_helper {
* can not be executed like this.
*
* @throws coding_exception
* @param string Context identifier (the class name).
* @param string $classname Context identifier (the class name).
* @return behat_base
*/
public static function get($classname) {

View File

@ -64,6 +64,7 @@ class behat_selectors {
'checkbox' => 'checkbox',
'radio' => 'radio',
'file' => 'file',
'filemanager' => 'filemanager',
'optgroup' => 'optgroup',
'option' => 'option',
'table' => 'table',
@ -98,6 +99,9 @@ XPATH
XPATH
, 'table_row' => <<<XPATH
.//tr[contains(normalize-space(.), %locator%)]
XPATH
, 'filemanager' => <<<XPATH
//div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]/descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
XPATH
);

View File

@ -50,6 +50,12 @@ class behat_form_field {
*/
protected $field;
/**
* @var string The field's locator.
*/
protected $fieldlocator = false;
/**
* General constructor with the node and the session to interact with.
*
@ -171,4 +177,63 @@ class behat_form_field {
}
return true;
}
/**
* Gets the field locator.
*
* Defaults to the field label but you can
* specify other locators if you are interested.
*
* Public visibility as in most cases will be hard to
* use this method in a generic way, as fields can
* be selected using multiple ways (label, id, name...).
*
* @throws coding_exception
* @param string $locatortype
* @return string
*/
protected function get_field_locator($locatortype = false) {
if (!empty($this->fieldlocator)) {
return $this->fieldlocator;
}
$fieldid = $this->field->getAttribute('id');
// Defaults to label.
if ($locatortype == 'label' || $locatortype == false) {
$labelnode = $this->session->getPage()->find('xpath', '//label[@for="' . $fieldid . '"]');
// Exception only if $locatortype was specified.
if (!$labelnode && $locatortype == 'label') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a label.');
}
$this->fieldlocator = $labelnode->getText();
}
// Let's look for the name as a second option (more popular than
// id's when pointing to fields).
if (($locatortype == 'name' || $locatortype == false) &&
empty($this->fieldlocator)) {
$name = $this->field->getAttribute('name');
// Exception only if $locatortype was specified.
if (!$name && $locatortype == 'name') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a name attribute.');
}
$this->fieldlocator = $name;
}
// Otherwise returns the id if no specific locator type was provided.
if (empty($this->fieldlocator)) {
$this->fieldlocator = $fieldid;
}
return $this->fieldlocator;
}
}

View File

@ -41,7 +41,7 @@ require_once(__DIR__ . '/behat_form_field.php');
*
* This field manager allows you to:
* - Get: A comma-separated list of the root directory
file names, including folders.
* file names, including folders.
* - Set: Add a file, in case you want to add more than
* one file you can always set two table rows using
* the same locator.

View File

@ -166,10 +166,8 @@ class behat_forms extends behat_base {
*/
public function the_field_matches_value($field, $value) {
$fieldnode = $this->find_field($field);
// Get the field.
$formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$formfield = behat_field_manager::get_form_field_from_label($field, $this);
// Checks if the provided value matches the current field value.
if (!$formfield->matches($value)) {
@ -193,10 +191,8 @@ class behat_forms extends behat_base {
*/
public function the_field_does_not_match_value($field, $value) {
$fieldnode = $this->find_field($field);
// Get the field.
$formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$formfield = behat_field_manager::get_form_field_from_label($field, $this);
// Checks if the provided value matches the current field value.
if ($formfield->matches($value)) {
@ -348,11 +344,9 @@ class behat_forms extends behat_base {
*/
protected function set_field_value($fieldlocator, $value) {
$node = $this->find_field($fieldlocator);
// We delegate to behat_form_field class, it will
// guess the type properly as it is a select tag.
$field = behat_field_manager::get_form_field($node, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
$field->set_value($value);
}

View File

@ -110,6 +110,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/behat_context_helper.php');
require_once(__DIR__ . '/../../behat/classes/util.php');
require_once(__DIR__ . '/../../testing/classes/test_lock.php');
require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
@ -184,6 +185,7 @@ class behat_hooks extends behat_base {
// 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($session);
behat_context_helper::set_session($session);
}
// Reset $SESSION.

View File

@ -1,20 +1,22 @@
@mod @mod_forum
@mod @mod_forum @_file_upload
Feature: Add forum activities and discussions
In order to discuss topics with other users
As a teacher
I need to add forum activities to moodle courses
@javascript
Scenario: Add a forum and a discussion
Scenario: Add a forum and a discussion attaching files
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@asd.com |
| student1 | Student | 1 | student1@asd.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
@ -22,7 +24,24 @@ Feature: Add forum activities and discussions
| Forum name | Test forum name |
| Forum type | Standard forum for general use |
| Description | Test forum description |
When I add a new discussion to "Test forum name" forum with:
And I add a new discussion to "Test forum name" forum with:
| Subject | Forum post 1 |
| Message | This is the body |
Then I should see "Test forum name"
And I log out
And I log in as "student1"
And I follow "Course 1"
When I add a new discussion to "Test forum name" forum with:
| Subject | Post with attachment |
| Message | This is the body |
| Attachment | lib/tests/fixtures/empty.txt |
And I reply "Forum post 1" post from "Test forum name" forum with:
| Subject | Reply with attachment |
| Message | This is the body |
| Attachment | lib/tests/fixtures/upload_users.csv |
Then I should see "Reply with attachment"
And I should see "upload_users.csv"
And I follow "Test forum name"
And I follow "Post with attachment"
And I should see "empty.txt"
And I follow "Edit"
And the field "Attachment" matches value "empty.txt"

View File

@ -281,11 +281,8 @@ class behat_filepicker extends behat_files {
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);
// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);
// Delegates to the field class.
$field->set_value($value);

View File

@ -143,11 +143,8 @@ class behat_repository_upload extends behat_files {
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);
// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);
// Delegates to the field class.
$field->set_value($value);