Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

NEW catch JavaScript errors

both AJAX and non-AJAX related
  • Loading branch information...
commit e7132c1a1976616706f118162151930e023e8dee 1 parent 95c8aa4
Michał Ochman authored
39 features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php
@@ -49,6 +49,45 @@ public function getSession($name = null)
49 49 }
50 50
51 51 /**
  52 + * @AfterStep ~@modal
  53 + *
  54 + * Excluding scenarios with @modal tag is required,
  55 + * because modal dialogs stop any JS interaction
  56 + */
  57 + public function appendErrorHandlerBeforeStep(StepEvent $event)
  58 + {
  59 + $javascript = <<<JS
  60 +window.onerror = function(msg) {
  61 + var body = document.getElementsByTagName('body')[0];
  62 + body.setAttribute('data-jserrors', '[captured JavaScript error] ' + msg);
  63 +}
  64 +if ('undefined' !== typeof window.jQuery) {
  65 + window.jQuery('body').ajaxError(function(event, jqxhr, settings, exception) {
  66 + window.onerror(event.type + ': ' + settings.type + ' ' + settings.url + ' ' + exception);
  67 + });
  68 +}
  69 +JS;
  70 +
  71 + $this->getSession()->executeScript($javascript);
  72 + }
  73 +
  74 + /**
  75 + * @AfterStep ~@modal
  76 + *
  77 + * Excluding scenarios with @modal tag is required,
  78 + * because modal dialogs stop any JS interaction
  79 + */
  80 + public function readErrorHandlerAfterStep(StepEvent $event)
  81 + {
  82 + $page = $this->getSession()->getPage();
  83 +
  84 + $jserrors = $page->find('xpath', '//body[@data-jserrors]');
  85 + if (null !== $jserrors) {
  86 + throw new \Exception($jserrors->getAttribute('data-jserrors'));
  87 + }
  88 + }
  89 +
  90 + /**
52 91 * Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events.
53 92 * Prepare __ajaxStatus() functions and attach them to these handlers.
54 93 * Event handlers are removed after one run.

0 comments on commit e7132c1

Please sign in to comment.
Something went wrong with that request. Please try again.