Skip to content

Commit

Permalink
MDL-41555 behat: Fixing catch statement location
Browse files Browse the repository at this point in the history
The exception is thrown when getting the
browser session, not when accessing the first
URL.
  • Loading branch information
David Monllao committed Sep 12, 2013
1 parent c1f10ff commit f59f6b6
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,23 @@ public function before_scenario($event) {
throw new coding_exception('Behat only can modify the test database and the test dataroot!');
}

try {
$session = $this->getSession();
} catch (CurlExec $e) {
// Exception thrown by WebDriver, so only @javascript tests will be caugth; in
// behat_util::is_server_running() we already checked that the server is running.
$moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
$msg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
throw new Exception($msg);
} catch (UnknownError $e) {
// Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
$this->throw_unknown_exception($e);
}


// 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($this->getSession());
behat_selectors::register_moodle_selectors($session);
}

// Avoid some notices / warnings.
Expand All @@ -168,25 +182,20 @@ public function before_scenario($event) {
if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
$now = time();
if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
$this->getSession()->restart();
$session->restart();
self::$lastbrowsersessionstart = $now;
}
}

// Start always in the the homepage.
try {
$this->getSession()->visit($this->locate_path('/'));
} catch (CurlExec $e) {
// Exception thrown by WebDriver, so only @javascript tests will be caugth; in
// behat_util::is_server_running() we already checked that the server is running.
$moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
$msg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
throw new Exception($msg);
// Let's be conservative as we never know when new upstream issues will affect us.
$session->visit($this->locate_path('/'));
} catch (UnknownError $e) {
// Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
$this->throw_unknown_exception($e);
}


// Checking that the root path is a Moodle test site.
if (self::is_first_scenario()) {
$notestsiteexception = new Exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' .
Expand All @@ -199,7 +208,7 @@ public function before_scenario($event) {
// Closing JS dialogs if present. Otherwise they would block this scenario execution.
if ($this->running_javascript()) {
try {
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
$session->getDriver()->getWebDriverSession()->accept_alert();
} catch (NoAlertOpenError $e) {
// All ok, there should not be JS dialogs in theory.
}
Expand Down

0 comments on commit f59f6b6

Please sign in to comment.