@@ -110,7 +110,7 @@ protected function add_new_discussion($forumname, TableNode $table, $buttonstr)
110110 // Fill form and post.
111111 $ this ->execute ('behat_forms::i_set_the_following_fields_to_these_values ' , $ table );
112112 $ this ->execute ('behat_forms::press_button ' , get_string ('posttoforum ' , 'hsuforum ' ));
113- $ this ->execute ('behat_general::i_wait_to_be_redirected ' );
113+ $ this ->execute ('behat_mod_hsuforum::i_wait_to_be_redirected_to_open_forum ' );
114114 }
115115
116116 /**
@@ -343,4 +343,57 @@ function($context, $iframename) {
343343 behat_base::get_extended_timeout ()
344344 );
345345 }
346+
347+ /**
348+ * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection.
349+ * Based on i_wait_to_be_redirected from behat_general.
350+ *
351+ * @Given /^I wait to be redirected to open forum$/
352+ */
353+ public function i_wait_to_be_redirected_to_open_forum () {
354+
355+ // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
356+ // moodle_page::$periodicrefreshdelay possible values.
357+ if (!$ metarefresh = $ this ->getSession ()->getPage ()->find ('xpath ' , "//head/descendant::meta[@http-equiv='refresh'] " )) {
358+ // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
359+ return true ;
360+ }
361+
362+ // Wrapped in try & catch in case the redirection has already been executed.
363+ try {
364+ $ content = $ metarefresh ->getAttribute ('content ' );
365+ } catch (NoSuchElementException $ e ) {
366+ return true ;
367+ } catch (StaleElementReferenceException $ e ) {
368+ return true ;
369+ }
370+
371+ // Getting the refresh time and the url if present.
372+ if (strstr ($ content , 'url ' ) != false ) {
373+
374+ list ($ waittime , $ url ) = explode ('; ' , $ content );
375+
376+ // Cleaning the URL value.
377+ $ url = trim (substr ($ url , strpos ($ url , 'http ' )));
378+
379+ } else {
380+ // Just wait then.
381+ $ waittime = $ content ;
382+ }
383+
384+
385+ // Wait until the URL change is executed.
386+ if ($ this ->running_javascript ()) {
387+ $ this ->getSession ()->wait ($ waittime * 1000 );
388+
389+ } else if (!empty ($ url )) {
390+ // We redirect directly as we can not wait for an automatic redirection.
391+ $ this ->getSession ()->getDriver ()->getClient ()->request ('GET ' , $ url );
392+
393+ } else {
394+ // Reload the page if no URL was provided.
395+ $ this ->getSession ()->getDriver ()->reload ();
396+ }
397+ }
398+
346399}
0 commit comments