diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 3f5b4ba57f2..e20cdc5e49c 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -66,10 +66,6 @@ class behat_course extends behat_base { */ public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) { - $activity = $this->fixStepArgument($activity); - $section = $this->fixStepArgument($section); - - // The 'I wait until the page is ready' is just in case. return array( new Given('I add a "'.$activity.'" to section "'.$section.'"'), new Given('I fill the moodle form with:', $data), @@ -81,6 +77,7 @@ class behat_course extends behat_base { * Opens the activity chooser and opens the activity/resource form page. * * @Given /^I add a "(?P(?:[^"]|\\")*)" to section "(?P\d+)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find * @param string $activity * @param string $section */ @@ -90,14 +87,14 @@ class behat_course extends behat_base { $section = $this->fixStepArgument($section); // Clicks add activity or resource section link. - $sectionxpath = "//*[@id='section-" . $section . "']/*/*/*/div[@class='section-modchooser']/*/*"; - $section = $this->getSession()->getPage()->find('xpath', $sectionxpath); - $section->click(); + $sectionxpath = "//*[@id='section-" . $section . "']/*/*/*/div[@class='section-modchooser']/span/a"; + $sectionnode = $this->find('xpath', $sectionxpath); + $sectionnode->click(); // Clicks the selected activity if it exists. $activityxpath = ".//label[contains(.,'" . $activity . "')]/input"; - $activity = $this->getSession()->getPage()->find('xpath', $activityxpath); - $activity->doubleClick(); + $activitynode = $this->find('xpath', $activityxpath); + $activitynode->doubleClick(); } } diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 2e6d386ce53..d409231ddec 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -222,7 +222,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('user', 'id', array('username' => $username))) { - throw new Exception('The specified user with username "' . $username . '" does not exists'); + throw new Exception('The specified user with username "' . $username . '" does not exist'); } return $id; } @@ -237,7 +237,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('role', 'id', array('shortname' => $roleshortname))) { - throw new Exception('The specified role with shortname"' . $roleshortname . '" does not exists'); + throw new Exception('The specified role with shortname"' . $roleshortname . '" does not exist'); } return $id; @@ -258,7 +258,7 @@ class behat_data_generators extends behat_base { } if (!$id = $DB->get_field('course_categories', 'id', array('idnumber' => $idnumber))) { - throw new Exception('The specified category with idnumber "' . $idnumber . '" does not exists'); + throw new Exception('The specified category with idnumber "' . $idnumber . '" does not exist'); } return $id; @@ -274,7 +274,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('course', 'id', array('shortname' => $shortname))) { - throw new Exception('The specified course with shortname"' . $shortname . '" does not exists'); + throw new Exception('The specified course with shortname"' . $shortname . '" does not exist'); } return $id; } @@ -289,7 +289,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('groups', 'id', array('idnumber' => $idnumber))) { - throw new Exception('The specified group with idnumber "' . $idnumber . '" does not exists'); + throw new Exception('The specified group with idnumber "' . $idnumber . '" does not exist'); } return $id; } @@ -304,7 +304,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('groupings', 'id', array('idnumber' => $idnumber))) { - throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exists'); + throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist'); } return $id; } diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 972b0160337..c9df3e9720e 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -48,19 +48,22 @@ class behat_forms extends behat_base { /** * Presses button with specified id|name|title|alt|value. * - * @see Behat\MinkExtension\Context\MinkContext * @When /^I press "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function press_button($button) { $button = $this->fixStepArgument($button); - $this->getSession()->getPage()->pressButton($button); + + // Ensures the button is present. + $buttonnode = $this->find_button($button); + $buttonnode->press(); } /** * Fills a moodle form with field/value data. * - * @throws ElementNotFoundException * @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) { @@ -75,21 +78,8 @@ class behat_forms extends behat_base { // Removing \\ that escapes " of the steps arguments. $locator = $this->fixStepArgument($locator); - // Finds the element in the page waiting until it appears (or timeouts) - // otherwise spin() throws exception. - $exception = new ElementNotFoundException( - $this->getSession(), 'form field', 'id|name|label|value', $locator - ); - - // $context is $this and will be passed to the function by spin(). - $args['locator'] = $locator; - - // Closure to ensure field($locator) exists. - $fieldnode = $this->spin( - function($context, $args) { - return $context->getSession()->getPage()->findField($args['locator']); - }, $exception, $args - ); + // Getting the NodeElement. + $fieldnode = $this->find_field($locator); // Gets the field type from a parent node. $field = $this->get_field($fieldnode, $locator); @@ -103,65 +93,66 @@ class behat_forms extends behat_base { /** * Fills in form field with specified id|name|label|value. * - * @see Behat\MinkExtension\Context\MinkContext * @When /^I fill in "(?P(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function fill_field($field, $value) { $field = $this->fixStepArgument($field); $value = $this->fixStepArgument($value); - $this->getSession()->getPage()->fillField($field, $value); + + $fieldnode = $this->find_field($field); + $fieldnode->setValue($value); } /** * Selects option in select field with specified id|name|label|value. * - * @see Behat\MinkExtension\Context\MinkContext - * @throws ElementNotFoundException * @When /^I select "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function select_option($option, $select) { $select = $this->fixStepArgument($select); $option = $this->fixStepArgument($option); - // We add the click event to deal with autosubmit drop down menus. - $selectnode = $this->getSession()->getPage()->findField($select); - if ($selectnode == null) { - throw new ElementNotFoundException( - $this->getSession(), 'form field', 'id|name|label|value', $select - ); - } + $selectnode = $this->find_field($select); $selectnode->selectOption($option); + + // Adding a click as Selenium requires it to fire some JS events. $selectnode->click(); } /** * Checks checkbox with specified id|name|label|value. * - * @see Behat\MinkExtension\Context\MinkContext * @When /^I check "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function check_option($option) { $option = $this->fixStepArgument($option); - $this->getSession()->getPage()->checkField($option); + + $checkboxnode = $this->find_field($option); + $checkboxnode->check(); } /** * Unchecks checkbox with specified id|name|label|value. * - * @see Behat\MinkExtension\Context\MinkContext * @When /^I uncheck "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function uncheck_option($option) { $option = $this->fixStepArgument($option); - $this->getSession()->getPage()->uncheckField($option); + + $checkboxnode = $this->find_field($option); + $checkboxnode->uncheck(); } /** * Checks that the form element field have the specified value. * - * @throws ElementNotFoundException - * @throws ExpectationException * @Then /^the "(?P(?:[^"]|\\")*)" field should match "(?P(?:[^"]|\\")*)" value$/ + * @throws ExpectationException + * @throws ElementNotFoundException Thrown by behat_base::find * @param mixed $locator * @param mixed $value */ @@ -170,12 +161,7 @@ class behat_forms extends behat_base { $locator = $this->fixStepArgument($locator); $value = $this->fixStepArgument($value); - $fieldnode = $this->getSession()->getPage()->findField($locator); - if (null === $fieldnode) { - throw new ElementNotFoundException( - $this->getSession(), 'form field', 'id|name|label|value', $locator - ); - } + $fieldnode = $this->find_field($locator); // Gets the field instance. $field = $this->get_field($fieldnode, $locator); @@ -192,8 +178,8 @@ class behat_forms extends behat_base { /** * Checks, that checkbox with specified in|name|label|value is checked. * - * @see Behat\MinkExtension\Context\MinkContext * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should be checked$/ + * @see Behat\MinkExtension\Context\MinkContext */ public function assert_checkbox_checked($checkbox) { $checkbox = $this->fixStepArgument($checkbox); @@ -203,8 +189,8 @@ class behat_forms extends behat_base { /** * Checks, that checkbox with specified in|name|label|value is unchecked. * - * @see Behat\MinkExtension\Context\MinkContext * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should not be checked$/ + * @see Behat\MinkExtension\Context\MinkContext */ public function assert_checkbox_not_checked($checkbox) { $checkbox = $this->fixStepArgument($checkbox); @@ -214,9 +200,9 @@ class behat_forms extends behat_base { /** * Checks, that given select box contains the specified option. * - * @throws ExpectationException - * @throws ElementNotFoundException * @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 */ @@ -225,12 +211,7 @@ class behat_forms extends behat_base { $select = $this->fixStepArgument($select); $option = $this->fixStepArgument($option); - $selectnode = $this->getSession()->getPage()->findField($select); - if ($selectnode == null) { - throw new ElementNotFoundException( - $this->getSession(), 'form field', 'id|name|label|value', $select - ); - } + $selectnode = $this->find_field($select); $regex = '/' . preg_quote($option, '/') . '/ui'; if (!preg_match($regex, $selectnode->getText())) { @@ -245,9 +226,9 @@ class behat_forms extends behat_base { /** * Checks, that given select box does not contain the specified option. * - * @throws ExpectationException - * @throws ElementNotFoundException * @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 */ @@ -256,12 +237,7 @@ class behat_forms extends behat_base { $select = $this->fixStepArgument($select); $option = $this->fixStepArgument($option); - $selectnode = $this->getSession()->getPage()->findField($select); - if ($selectnode == null) { - throw new ElementNotFoundException( - $this->getSession(), 'form field', 'id|name|label|value', $select - ); - } + $selectnode = $this->find_field($select); $regex = '/' . preg_quote($option, '/') . '/ui'; if (preg_match($regex, $selectnode->getText())) { diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 0b178b957ee..03d1a80f15d 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -47,7 +47,6 @@ class behat_general extends behat_base { /** * Opens Moodle homepage. * - * @see Behat\MinkExtension\Context\MinkContext * @Given /^I am on homepage$/ */ public function i_am_on_homepage() { @@ -57,12 +56,14 @@ class behat_general extends behat_base { /** * Clicks link with specified id|title|alt|text. * - * @see Behat\MinkExtension\Context\MinkContext * @When /^I follow "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find */ public function click_link($link) { $link = $this->fixStepArgument($link); - $this->getSession()->getPage()->clickLink($link); + + $linknode = $this->find_link($link); + $linknode->click(); } /** @@ -87,16 +88,17 @@ class behat_general extends behat_base { /** * Mouse over a CSS element. * - * @throws ExpectationException - * @see Sanpi/Behatch/Context/BrowserContext.php * @When /^I hover "(?P(?:[^"]|\\")*)"$/ + * @throws ExpectationException Thrown by behat_base::find * @param string $element */ public function i_hover($element) { - $node = $this->getSession()->getPage()->find('css', $element); - if ($node === null) { - throw new ExpectationException('The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession()); - } + + $exception = new ExpectationException( + 'The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession() + ); + + $node = $this->find('css', $element, $exception); $node->mouseOver(); } @@ -126,7 +128,6 @@ class behat_general extends behat_base { * @Then /^I should see "(?P(?:[^"]|\\")*)" in the "(?P(?:[^"]|\\")*)" element$/ */ public function assert_element_contains_text($element, $text) { - $element = $this->fixStepArgument($element); $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text)); } @@ -136,26 +137,20 @@ class behat_general extends behat_base { * @Then /^I should not see "(?P(?:[^"]|\\")*)" in the "(?P(?:[^"]|\\")*)" element$/ */ public function assert_element_not_contains_text($element, $text) { - $element = $this->fixStepArgument($element); $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text)); } /** * Checks, that element with given CSS is disabled. * - * @throws ExpectationException - * @see Sanpi/Behatch/Context/BrowserContext * @Then /^the element "(?P(?:[^"]|\\")*)" should be disabled$/ + * @throws ExpectationException Thrown by behat_base::find * @param string $element */ public function the_element_should_be_disabled($element) { - $element = $this->fixStepArgument($element); - - $node = $this->getSession()->getPage()->find('css', $element); - if ($node == null) { - throw new ExpectationException('There is no "' . $element . '" element', $this->getSession()); - } + $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession()); + $node = $this->find('css', $element, $exception); if (!$node->hasAttribute('disabled')) { throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession()); @@ -165,16 +160,14 @@ class behat_general extends behat_base { /** * Checks, that element with given CSS is enabled. * - * @throws ExpectationException - * @see Sanpi/Behatch/Context/BrowserContext.php * @Then /^the element "(?P(?:[^"]|\\")*)" should be enabled$/ + * @throws ExpectationException Thrown by behat_base::find * @param string $element */ public function the_element_should_be_enabled($element) { - $node = $this->getSession()->getPage()->find('css', $element); - if ($node == null) { - throw new ExpectationException('There is no "' . $element . '" element', $this->getSession()); - } + + $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession()); + $node = $this->find('css', $element, $exception); if ($node->hasAttribute('disabled')) { throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession()); diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 169e67d9ae7..ad5031e8de3 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -40,8 +40,9 @@ class behat_navigation extends behat_base { /** * Expands the selected node of the navigation tree that matches the text. - * * @Given /^I expand "(?P(?:[^"]|\\")*)" node$/ + * + * @throws ElementNotFoundException Thrown by behat_base::find * @param string $nodetext */ public function i_expand_node($nodetext) { @@ -53,7 +54,7 @@ class behat_navigation extends behat_base { /descendant::p[contains(concat(' ', normalize-space(@class), ' '), ' branch')] /descendant::span[contains(concat(' ', normalize-space(.), ' '), '" . $nodetext . "')]"; - $node = $this->getSession()->getPage()->find('xpath', $xpath); + $node = $this->find('xpath', $xpath); $node->click(); }