Merge pull request #97 from IgorNadj/patch-safety

ENH: adding safety try-catch blocks to prevent whole test suite from falling over
This commit is contained in:
Ingo Schommer 2015-09-10 14:58:41 +12:00
commit 2a1e1e29d6

View File

@ -76,6 +76,7 @@ class BasicContext extends BehatContext
* because modal dialogs stop any JS interaction * because modal dialogs stop any JS interaction
*/ */
public function appendErrorHandlerBeforeStep(StepEvent $event) { public function appendErrorHandlerBeforeStep(StepEvent $event) {
try{
$javascript = <<<JS $javascript = <<<JS
window.onerror = function(message, file, line, column, error) { window.onerror = function(message, file, line, column, error) {
var body = document.getElementsByTagName('body')[0]; var body = document.getElementsByTagName('body')[0];
@ -94,6 +95,9 @@ if ('undefined' !== typeof window.jQuery) {
JS; JS;
$this->getSession()->executeScript($javascript); $this->getSession()->executeScript($javascript);
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
/** /**
@ -103,6 +107,7 @@ JS;
* because modal dialogs stop any JS interaction * because modal dialogs stop any JS interaction
*/ */
public function readErrorHandlerAfterStep(StepEvent $event) { public function readErrorHandlerAfterStep(StepEvent $event) {
try{
$page = $this->getSession()->getPage(); $page = $this->getSession()->getPage();
$jserrors = $page->find('xpath', '//body[@data-jserrors]'); $jserrors = $page->find('xpath', '//body[@data-jserrors]');
@ -120,6 +125,9 @@ if ('undefined' !== typeof window.jQuery) {
JS; JS;
$this->getSession()->executeScript($javascript); $this->getSession()->executeScript($javascript);
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
/** /**
@ -130,6 +138,7 @@ JS;
* @BeforeStep * @BeforeStep
*/ */
public function handleAjaxBeforeStep(StepEvent $event) { public function handleAjaxBeforeStep(StepEvent $event) {
try{
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
@ -162,6 +171,9 @@ if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery
JS; JS;
$this->getSession()->wait(500); // give browser a chance to process and render response $this->getSession()->wait(500); // give browser a chance to process and render response
$this->getSession()->executeScript($javascript); $this->getSession()->executeScript($javascript);
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
/** /**
@ -173,6 +185,7 @@ JS;
* @AfterStep ~@modal * @AfterStep ~@modal
*/ */
public function handleAjaxAfterStep(StepEvent $event) { public function handleAjaxAfterStep(StepEvent $event) {
try{
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
@ -190,6 +203,9 @@ window.jQuery(document).off('ajaxSuccess.ss.test.behaviour');
} }
JS; JS;
$this->getSession()->executeScript($javascript); $this->getSession()->executeScript($javascript);
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
public function handleAjaxTimeout() { public function handleAjaxTimeout() {
@ -212,7 +228,11 @@ JS;
*/ */
public function takeScreenshotAfterFailedStep(StepEvent $event) { public function takeScreenshotAfterFailedStep(StepEvent $event) {
if (4 === $event->getResult()) { if (4 === $event->getResult()) {
try{
$this->takeScreenshot($event); $this->takeScreenshot($event);
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
} }
@ -222,6 +242,7 @@ JS;
* @AfterScenario * @AfterScenario
*/ */
public function closeModalDialog(ScenarioEvent $event) { public function closeModalDialog(ScenarioEvent $event) {
try{
// Only for failed tests on CMS page // Only for failed tests on CMS page
if (4 === $event->getResult()) { if (4 === $event->getResult()) {
$cmsElement = $this->getSession()->getPage()->find('css', '.cms'); $cmsElement = $this->getSession()->getPage()->find('css', '.cms');
@ -235,6 +256,9 @@ JS;
} }
} }
} }
}catch(\WebDriver\Exception $e){
$this->logException($e);
}
} }
/** /**
@ -942,4 +966,14 @@ JS;
$backtrace[1]['function'] $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);
}
} }