diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index c30c4c31e52..db0e2261d63 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -28,7 +28,8 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); use Behat\Mink\Exception\ExpectationException as ExpectationException, - Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; + Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, + Behat\Mink\Exception\DriverException as DriverException; /** * Cross component steps definitions. @@ -63,6 +64,48 @@ class behat_general extends behat_base { $this->getSession()->reload(); } + /** + * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection + * + * @Given /^I wait to be redirected$/ + */ + public function i_wait_to_be_redirected() { + + // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and + // moodle_page::$periodicrefreshdelay possible values. + if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) { + // We don't fail the scenario if no redirection with message is found to avoid race condition false failures. + return false; + } + + $content = $metarefresh->getAttribute('content'); + if (strstr($content, 'url') != false) { + + list($waittime, $url) = explode(';', $metarefresh->getAttribute('content')); + + // Cleaning the URL value. + $url = trim(substr($url, strpos($url, 'http'))); + + } else { + // Just wait then. + $waittime = $content; + } + + + // Wait until the URL change is executed. + if ($this->running_javascript()) { + $this->getSession()->wait($waittime * 1000, false); + + } else if (!empty($url)) { + // We redirect directly as we can not wait for an automatic redirection. + $this->getSession()->getDriver()->getClient()->request('get', $url); + + } else { + // Reload the page if no URL was provided. + $this->getSession()->getDriver()->reload(); + } + } + /** * Switches to the specified window. Useful when interacting with popup windows. * @@ -110,6 +153,11 @@ class behat_general extends behat_base { * @param int $seconds */ public function i_wait_seconds($seconds) { + + if (!$this->running_javascript()) { + throw new DriverException('Waits are disabled in scenarios without Javascript support'); + } + $this->getSession()->wait($seconds * 1000, false); } @@ -119,6 +167,11 @@ class behat_general extends behat_base { * @Given /^I wait until the page is ready$/ */ public function wait_until_the_page_is_ready() { + + if (!$this->running_javascript()) { + throw new DriverException('Waits are disabled in scenarios without Javascript support'); + } + $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); } diff --git a/mod/forum/tests/behat/behat_mod_forum.php b/mod/forum/tests/behat/behat_mod_forum.php index 8ddf100678e..e5afd877d92 100644 --- a/mod/forum/tests/behat/behat_mod_forum.php +++ b/mod/forum/tests/behat/behat_mod_forum.php @@ -51,10 +51,10 @@ class behat_mod_forum extends behat_base { // Escaping $forumname as it has been stripped automatically by the transformer. return array( new Given('I follow "' . $this->escape($forumname) . '"'), - new Given('I press "Add a new discussion topic"'), + new Given('I press "' . get_string('addanewdiscussion', 'mod_forum') . '"'), new Given('I fill the moodle form with:', $table), - new Given('I press "Post to forum"'), - new Given('I wait "5" seconds') + new Given('I press "' . get_string('posttoforum', 'mod_forum') . '"'), + new Given('I wait to be redirected') ); } @@ -62,8 +62,8 @@ class behat_mod_forum extends behat_base { * Adds a reply to the specified post of the specified forum. The step begins from the forum's page or from the forum's course page. * * @Given /^I reply "(?P(?:[^"]|\\")*)" post from "(?P(?:[^"]|\\")*)" forum with:$/ - * @param mixed $postname The subject of the post - * @param mixed $forumname The forum name + * @param string $postname The subject of the post + * @param string $forumname The forum name * @param TableNode $table */ public function i_reply_post_from_forum_with($postsubject, $forumname, TableNode $table) { @@ -71,10 +71,11 @@ class behat_mod_forum extends behat_base { return array( new Given('I follow "' . $this->escape($forumname) . '"'), new Given('I follow "' . $this->escape($postsubject) . '"'), - new Given('I follow "Reply"'), + new Given('I follow "' . get_string('reply', 'mod_forum') . '"'), new Given('I fill the moodle form with:', $table), - new Given('I press "Post to forum"'), - new Given('I wait "5" seconds') + new Given('I press "' . get_string('posttoforum', 'mod_forum') . '"'), + new Given('I wait to be redirected') ); + } } diff --git a/mod/forum/tests/behat/edit_post_student.feature b/mod/forum/tests/behat/edit_post_student.feature index ea196f4b98a..e4b2ae4a2ac 100644 --- a/mod/forum/tests/behat/edit_post_student.feature +++ b/mod/forum/tests/behat/edit_post_student.feature @@ -33,9 +33,7 @@ Feature: Students can edit or delete their forum posts within a set time limit And I add a new discussion to "Test forum name" forum with: | Subject | Forum post subject | | Message | This is the body | - And I wait "6" seconds - @javascript Scenario: Edit forum post When I follow "Forum post subject" And I follow "Edit" @@ -43,7 +41,7 @@ Feature: Students can edit or delete their forum posts within a set time limit | Subject | Edited post subject | | Message | Edited post body | And I press "Save changes" - And I wait "6" seconds + And I wait to be redirected Then I should see "Edited post subject" And I should see "Edited post body"