From 8088c29f5a21546c7d53025198061296b7566dc2 Mon Sep 17 00:00:00 2001 From: Igor Nadj Date: Thu, 10 Sep 2015 14:32:09 +1200 Subject: [PATCH] ENH: adding safety try-catch blocks to prevent whole test suite from falling over --- .../BehatExtension/Context/BasicContext.php | 112 ++++++++++++------ 1 file changed, 73 insertions(+), 39 deletions(-) diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 200ce24..31cebed 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -76,7 +76,8 @@ class BasicContext extends BehatContext * because modal dialogs stop any JS interaction */ public function appendErrorHandlerBeforeStep(StepEvent $event) { - $javascript = <<getSession()->executeScript($javascript); + $this->getSession()->executeScript($javascript); + }catch(\WebDriver\Exception $e){ + $this->logException($e); + } } /** @@ -103,15 +107,16 @@ JS; * because modal dialogs stop any JS interaction */ public function readErrorHandlerAfterStep(StepEvent $event) { - $page = $this->getSession()->getPage(); - - $jserrors = $page->find('xpath', '//body[@data-jserrors]'); - if (null !== $jserrors) { - $this->takeScreenshot($event); - file_put_contents('php://stderr', $jserrors->getAttribute('data-jserrors') . PHP_EOL); - } - - $javascript = <<getSession()->getPage(); + + $jserrors = $page->find('xpath', '//body[@data-jserrors]'); + if (null !== $jserrors) { + $this->takeScreenshot($event); + file_put_contents('php://stderr', $jserrors->getAttribute('data-jserrors') . PHP_EOL); + } + + $javascript = <<getSession()->executeScript($javascript); + $this->getSession()->executeScript($javascript); + }catch(\WebDriver\Exception $e){ + $this->logException($e); + } } /** @@ -130,14 +138,15 @@ JS; * @BeforeStep */ public function handleAjaxBeforeStep(StepEvent $event) { - $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); - $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); + try{ + $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); + $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); - if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { - return; - } + if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { + return; + } - $javascript = <<getSession()->wait(500); // give browser a chance to process and render response - $this->getSession()->executeScript($javascript); + $this->getSession()->wait(500); // give browser a chance to process and render response + $this->getSession()->executeScript($javascript); + }catch(\WebDriver\Exception $e){ + $this->logException($e); + } } /** @@ -173,23 +185,27 @@ JS; * @AfterStep ~@modal */ public function handleAjaxAfterStep(StepEvent $event) { - $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); - $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); + try{ + $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); + $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); - if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { - return; - } + if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { + return; + } - $this->handleAjaxTimeout(); + $this->handleAjaxTimeout(); - $javascript = <<getSession()->executeScript($javascript); + $this->getSession()->executeScript($javascript); + }catch(\WebDriver\Exception $e){ + $this->logException($e); + } } public function handleAjaxTimeout() { @@ -212,7 +228,11 @@ JS; */ public function takeScreenshotAfterFailedStep(StepEvent $event) { if (4 === $event->getResult()) { - $this->takeScreenshot($event); + try{ + $this->takeScreenshot($event); + }catch(\WebDriver\Exception $e){ + $this->logException($e); + } } } @@ -222,18 +242,22 @@ JS; * @AfterScenario */ public function closeModalDialog(ScenarioEvent $event) { - // Only for failed tests on CMS page - if (4 === $event->getResult()) { - $cmsElement = $this->getSession()->getPage()->find('css', '.cms'); - if($cmsElement) { - try { - // Navigate away triggered by reloading the page - $this->getSession()->reload(); - $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); - } catch(\WebDriver\Exception $e) { - // no-op, alert might not be present + try{ + // Only for failed tests on CMS page + if (4 === $event->getResult()) { + $cmsElement = $this->getSession()->getPage()->find('css', '.cms'); + if($cmsElement) { + try { + // Navigate away triggered by reloading the page + $this->getSession()->reload(); + $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); + } catch(\WebDriver\Exception $e) { + // no-op, alert might not be present + } } } + }catch(\WebDriver\Exception $e){ + $this->logException($e); } } @@ -942,4 +966,14 @@ JS; $backtrace[1]['function'] )); } + + + + /** + * We have to catch exceptions and log somehow else otherwise behat falls over + */ + protected function logException($e){ + file_put_contents('php://stderr', 'Exception caught: '.$e); + } + }