. /** * Steps definitions related with forms. * * @package core * @category test * @copyright 2012 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php'); use Behat\Behat\Context\Step\Given as Given, Behat\Behat\Context\Step\When as When, Behat\Behat\Context\Step\Then as Then, Behat\Gherkin\Node\TableNode as TableNode, Behat\Mink\Element\NodeElement as NodeElement, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** * Forms-related steps definitions. * * @package core * @category test * @copyright 2012 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_forms extends behat_base { /** * Presses button with specified id|name|title|alt|value. * * @When /^I press "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $button */ public function press_button($button) { // Ensures the button is present. $buttonnode = $this->find_button($button); $buttonnode->press(); } /** * Fills a moodle form with field/value data. * * @Given /^I fill the moodle form with:$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param TableNode $data */ public function i_fill_the_moodle_form_with(TableNode $data) { // Expand all fields in case we have. $this->expand_all_fields(); $datahash = $data->getRowsHash(); // 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()); // Delegates to the field class. $field->set_value($value); } } /** * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present. * @Given /^I expand all fieldsets$/ */ public function i_expand_all_fieldsets() { $this->expand_all_fields(); } /** * Expands all moodle form fieldsets if they exists. * * Externalized from i_expand_all_fields to call it from * other form-related steps without having to use steps-group calls. * * @throws ElementNotFoundException Thrown by behat_base::find_all * @return void */ protected function expand_all_fields() { // We ensure that all the editors are loaded and we can interact with them. $this->ensure_editors_are_loaded(); // behat_base::find() throws an exception if there are no elements, we should not fail a test because of this. try { // Expand fieldsets link. $collapseexpandlink = $this->find('xpath', "//div[@class='collapsible-actions']" . "/descendant::a[contains(concat(' ', @class, ' '), ' collapseexpand ')]" . "[not(contains(concat(' ', @class, ' '), ' collapse-all '))]" ); $collapseexpandlink->click(); } catch (ElementNotFoundException $e) { // We continue if there are not expandable fields. } // Different try & catch as we can have expanded fieldsets with advanced fields on them. try { // Expand all fields xpath. $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" . "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]"; // We don't wait here as we already waited when getting the expand fieldsets links. $showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath); // Funny thing about this, with findAll() we specify a pattern and each element matching the pattern is added to the array // with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath // anymore (now is a "Show less..." link) so [1] becomes [0], that's why we always click on the first XPath match, // will be always the next one. $iterations = count($showmores); for ($i = 0; $i < $iterations; $i++) { $showmores[0]->click(); } } catch (ElementNotFoundException $e) { // We continue with the test. } } /** * Fills in form field with specified id|name|label|value. * * @When /^I fill in "(?P(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $field * @param string $value */ public function fill_field($field, $value) { $fieldnode = $this->find_field($field); $fieldnode->setValue($value); } /** * Selects option in select field with specified id|name|label|value. * * @When /^I select "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $option * @param string $select */ public function select_option($option, $select) { $selectnode = $this->find_field($select); // We delegate to behat_form_field class, it will // guess the type properly as it is a select tag. $selectformfield = behat_field_manager::get_form_field($selectnode, $this->getSession()); $selectformfield->set_value($option); } /** * Selects the specified id|name|label from the specified radio button. * * @When /^I select "(?P(?:[^"]|\\")*)" radio button$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $radio The radio button id, name or label value */ public function select_radio($radio) { $radionode = $this->find_radio($radio); $radionode->check(); // Adding a click as Selenium requires it to fire some JS events. if ($this->running_javascript()) { $radionode->click(); } } /** * Checks checkbox with specified id|name|label|value. * * @When /^I check "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $option */ public function check_option($option) { // We don't delegate to behat_form_checkbox as the // step is explicitly saying I check. $checkboxnode = $this->find_field($option); $checkboxnode->check(); } /** * Unchecks checkbox with specified id|name|label|value. * * @When /^I uncheck "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $option */ public function uncheck_option($option) { // We don't delegate to behat_form_checkbox as the // step is explicitly saying I uncheck. $checkboxnode = $this->find_field($option); $checkboxnode->uncheck(); } /** * Checks that the form element field have the specified value. * * @Then /^the "(?P(?:[^"]|\\")*)" field should match "(?P(?:[^"]|\\")*)" value$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $locator * @param string $value */ public function the_field_should_match_value($locator, $value) { $fieldnode = $this->find_field($locator); // Get the field. $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); $fieldvalue = $field->get_value(); // Checks if the provided value matches the current field value. if (trim($value) != trim($fieldvalue)) { throw new ExpectationException( 'The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' , $this->getSession() ); } } /** * Checks, that checkbox with specified in|name|label|value is checked. * * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should be checked$/ * @see Behat\MinkExtension\Context\MinkContext * @param string $checkbox */ public function assert_checkbox_checked($checkbox) { $this->assertSession()->checkboxChecked($checkbox); } /** * Checks, that checkbox with specified in|name|label|value is unchecked. * * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should not be checked$/ * @see Behat\MinkExtension\Context\MinkContext * @param string $checkbox */ public function assert_checkbox_not_checked($checkbox) { $this->assertSession()->checkboxNotChecked($checkbox); } /** * Checks, that given select box contains the specified option. * * @Then /^the "(?P(?:[^"]|\\")*)" select box should contain "(?P(?:[^"]|\\")*)"$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $select The select element name * @param string $option The option text/value */ public function the_select_box_should_contain($select, $option) { $selectnode = $this->find_field($select); $regex = '/' . preg_quote($option, '/') . '/ui'; if (!preg_match($regex, $selectnode->getText())) { throw new ExpectationException( 'The select box "' . $select . '" does not contains the option "' . $option . '"', $this->getSession() ); } } /** * Checks, that given select box does not contain the specified option. * * @Then /^the "(?P(?:[^"]|\\")*)" select box should not contain "(?P(?:[^"]|\\")*)"$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $select The select element name * @param string $option The option text/value */ public function the_select_box_should_not_contain($select, $option) { $selectnode = $this->find_field($select); $regex = '/' . preg_quote($option, '/') . '/ui'; if (preg_match($regex, $selectnode->getText())) { throw new ExpectationException( 'The select box "' . $select . '" contains the option "' . $option . '"', $this->getSession() ); } } }