Permalink
Browse files

NEW catch JavaScript errors

both AJAX and non-AJAX related
  • Loading branch information...
1 parent 95c8aa4 commit e7132c1a1976616706f118162151930e023e8dee @michalochman committed Aug 13, 2012
Showing with 39 additions and 0 deletions.
  1. +39 −0 features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php
View
39 features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php
@@ -49,6 +49,45 @@ public function getSession($name = null)
}
/**
+ * @AfterStep ~@modal
+ *
+ * Excluding scenarios with @modal tag is required,
+ * because modal dialogs stop any JS interaction
+ */
+ public function appendErrorHandlerBeforeStep(StepEvent $event)
+ {
+ $javascript = <<<JS
+window.onerror = function(msg) {
+ var body = document.getElementsByTagName('body')[0];
+ body.setAttribute('data-jserrors', '[captured JavaScript error] ' + msg);
+}
+if ('undefined' !== typeof window.jQuery) {
+ window.jQuery('body').ajaxError(function(event, jqxhr, settings, exception) {
+ window.onerror(event.type + ': ' + settings.type + ' ' + settings.url + ' ' + exception);
+ });
+}
+JS;
+
+ $this->getSession()->executeScript($javascript);
+ }
+
+ /**
+ * @AfterStep ~@modal
+ *
+ * Excluding scenarios with @modal tag is required,
+ * 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) {
+ throw new \Exception($jserrors->getAttribute('data-jserrors'));
+ }
+ }
+
+ /**
* Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events.
* Prepare __ajaxStatus() functions and attach them to these handlers.
* Event handlers are removed after one run.

0 comments on commit e7132c1

Please sign in to comment.