ENH: adding safety try-catch blocks to prevent whole test suite from falling over

This commit is contained in:
Igor Nadj 2015-09-10 14:32:09 +12:00
parent e26f0a2ba5
commit 8088c29f5a

View File

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