From bee40dbf271bfa7ba6724b6d1aed663e26d5378b Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 6 Mar 2019 10:28:16 +0800 Subject: [PATCH] MDL-63524 behat: Do not skip tests on Step 0 error --- lib/tests/behat/behat_hooks.php | 64 ++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 3b523f12755..a869ea06548 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -37,6 +37,7 @@ use Behat\Testwork\Hook\Scope\BeforeSuiteScope, Behat\Behat\Hook\Scope\AfterScenarioScope, Behat\Behat\Hook\Scope\BeforeStepScope, Behat\Behat\Hook\Scope\AfterStepScope, + Behat\Mink\Exception\ExpectationException, Behat\Mink\Exception\DriverException as DriverException, WebDriver\Exception\NoSuchWindow as NoSuchWindow, WebDriver\Exception\UnexpectedAlertOpen as UnexpectedAlertOpen, @@ -72,6 +73,11 @@ class behat_hooks extends behat_base { */ protected static $initprocessesfinished = false; + /** + * @var bool Scenario running + */ + protected $scenariorunning = false; + /** * Some exceptions can only be caught in a before or after step hook, * they can not be thrown there as they will provoke a framework level @@ -370,22 +376,8 @@ class behat_hooks extends behat_base { self::$runningsuite = $suitename; } - // Start always in the the homepage. - try { - // Let's be conservative as we never know when new upstream issues will affect us. - $session->visit($this->locate_path('/')); - } catch (UnknownError $e) { - throw new behat_stop_exception($e->getMessage()); - } - - // Checking that the root path is a Moodle test site. - if (self::is_first_scenario()) { - $notestsiteexception = new behat_stop_exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . - 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up'); - $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception); - - self::$initprocessesfinished = true; - } + // Reset the scenariorunning variable to ensure that Step 0 occurs. + $this->scenariorunning = false; // Run all test with medium (1024x768) screen size, to avoid responsive problems. $this->resize_window('medium'); @@ -399,6 +391,46 @@ class behat_hooks extends behat_base { } } + /** + * Hook to open the site root before the first step in the suite. + * Yes, this is in a strange location and should be in the BeforeScenario hook, but failures in the test setUp lead + * to the test being incorrectly marked as skipped with no way to force the test to be failed. + * + * @param BeforeStepScope $scope + * @BeforeStep + */ + public function before_step(BeforeStepScope $scope) { + global $CFG; + + if (!$this->scenariorunning) { + // We need to visit / before the first step in any Scenario. + // This is our Step 0. + // Ideally this would be in the BeforeScenario hook, but any exception in there will lead to the test being + // skipped rather than it being failed. + // + // We also need to check that the site returned is a Behat site. + // Again, this would be better in the BeforeSuite hook, but that does not have access to the selectors in + // order to perform the necessary searches. + $session = $this->getSession(); + $session->visit($this->locate_path('/')); + + // Checking that the root path is a Moodle test site. + if (self::is_first_scenario()) { + $message = "The base URL ({$CFG->wwwroot}) is not a behat test site. " . + 'Ensure that you started the built-in web server in the correct directory, ' . + 'or that your web server is correctly set up and started.'; + + $this->find( + "xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", + new ExpectationException($message, $session) + ); + + self::$initprocessesfinished = true; + } + $this->scenariorunning = true; + } + } + /** * Sets up the tags for the current scenario. *