mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 17:05:32 +02:00
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:
commit
2a1e1e29d6
@ -76,7 +76,8 @@ 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) {
|
||||||
$javascript = <<<JS
|
try{
|
||||||
|
$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];
|
||||||
var msg = message + " in " + file + ":" + line + ":" + column;
|
var msg = message + " in " + file + ":" + line + ":" + column;
|
||||||
@ -93,7 +94,10 @@ if ('undefined' !== typeof window.jQuery) {
|
|||||||
}
|
}
|
||||||
JS;
|
JS;
|
||||||
|
|
||||||
$this->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
|
* because modal dialogs stop any JS interaction
|
||||||
*/
|
*/
|
||||||
public function readErrorHandlerAfterStep(StepEvent $event) {
|
public function readErrorHandlerAfterStep(StepEvent $event) {
|
||||||
$page = $this->getSession()->getPage();
|
try{
|
||||||
|
$page = $this->getSession()->getPage();
|
||||||
$jserrors = $page->find('xpath', '//body[@data-jserrors]');
|
|
||||||
if (null !== $jserrors) {
|
$jserrors = $page->find('xpath', '//body[@data-jserrors]');
|
||||||
$this->takeScreenshot($event);
|
if (null !== $jserrors) {
|
||||||
file_put_contents('php://stderr', $jserrors->getAttribute('data-jserrors') . PHP_EOL);
|
$this->takeScreenshot($event);
|
||||||
}
|
file_put_contents('php://stderr', $jserrors->getAttribute('data-jserrors') . PHP_EOL);
|
||||||
|
}
|
||||||
$javascript = <<<JS
|
|
||||||
|
$javascript = <<<JS
|
||||||
if ('undefined' !== typeof window.jQuery) {
|
if ('undefined' !== typeof window.jQuery) {
|
||||||
window.jQuery(document).ready(function() {
|
window.jQuery(document).ready(function() {
|
||||||
window.jQuery('body').removeAttr('data-jserrors');
|
window.jQuery('body').removeAttr('data-jserrors');
|
||||||
@ -119,7 +124,10 @@ if ('undefined' !== typeof window.jQuery) {
|
|||||||
}
|
}
|
||||||
JS;
|
JS;
|
||||||
|
|
||||||
$this->getSession()->executeScript($javascript);
|
$this->getSession()->executeScript($javascript);
|
||||||
|
}catch(\WebDriver\Exception $e){
|
||||||
|
$this->logException($e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,14 +138,15 @@ JS;
|
|||||||
* @BeforeStep
|
* @BeforeStep
|
||||||
*/
|
*/
|
||||||
public function handleAjaxBeforeStep(StepEvent $event) {
|
public function handleAjaxBeforeStep(StepEvent $event) {
|
||||||
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
|
try{
|
||||||
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
|
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
|
||||||
|
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
|
||||||
|
|
||||||
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
|
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$javascript = <<<JS
|
$javascript = <<<JS
|
||||||
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.on) {
|
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.on) {
|
||||||
window.jQuery(document).on('ajaxStart.ss.test.behaviour', function(){
|
window.jQuery(document).on('ajaxStart.ss.test.behaviour', function(){
|
||||||
window.__ajaxStatus = function() {
|
window.__ajaxStatus = function() {
|
||||||
@ -160,8 +169,11 @@ 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,23 +185,27 @@ JS;
|
|||||||
* @AfterStep ~@modal
|
* @AfterStep ~@modal
|
||||||
*/
|
*/
|
||||||
public function handleAjaxAfterStep(StepEvent $event) {
|
public function handleAjaxAfterStep(StepEvent $event) {
|
||||||
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
|
try{
|
||||||
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
|
$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
|
||||||
|
$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
|
||||||
|
|
||||||
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
|
if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handleAjaxTimeout();
|
$this->handleAjaxTimeout();
|
||||||
|
|
||||||
$javascript = <<<JS
|
$javascript = <<<JS
|
||||||
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.off) {
|
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.off) {
|
||||||
window.jQuery(document).off('ajaxStart.ss.test.behaviour');
|
window.jQuery(document).off('ajaxStart.ss.test.behaviour');
|
||||||
window.jQuery(document).off('ajaxComplete.ss.test.behaviour');
|
window.jQuery(document).off('ajaxComplete.ss.test.behaviour');
|
||||||
window.jQuery(document).off('ajaxSuccess.ss.test.behaviour');
|
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()) {
|
||||||
$this->takeScreenshot($event);
|
try{
|
||||||
|
$this->takeScreenshot($event);
|
||||||
|
}catch(\WebDriver\Exception $e){
|
||||||
|
$this->logException($e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,18 +242,22 @@ JS;
|
|||||||
* @AfterScenario
|
* @AfterScenario
|
||||||
*/
|
*/
|
||||||
public function closeModalDialog(ScenarioEvent $event) {
|
public function closeModalDialog(ScenarioEvent $event) {
|
||||||
// Only for failed tests on CMS page
|
try{
|
||||||
if (4 === $event->getResult()) {
|
// Only for failed tests on CMS page
|
||||||
$cmsElement = $this->getSession()->getPage()->find('css', '.cms');
|
if (4 === $event->getResult()) {
|
||||||
if($cmsElement) {
|
$cmsElement = $this->getSession()->getPage()->find('css', '.cms');
|
||||||
try {
|
if($cmsElement) {
|
||||||
// Navigate away triggered by reloading the page
|
try {
|
||||||
$this->getSession()->reload();
|
// Navigate away triggered by reloading the page
|
||||||
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
|
$this->getSession()->reload();
|
||||||
} catch(\WebDriver\Exception $e) {
|
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
|
||||||
// no-op, alert might not be present
|
} 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']
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user