Created
September 24, 2015 19:28
-
-
Save basilfx/49405f8a1642318a6c52 to your computer and use it in GitHub Desktop.
JavaScript errors in Behat 3 + Mink 2 + Selenium 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Include this file somewhere after in the top of your page. | |
*/ | |
/** | |
* @var Array List of all errors occurred. | |
*/ | |
var errors = []; | |
/** | |
* Add one error to the list of errors. | |
*/ | |
function addError(error) { | |
// Ensure error is an object, otherwise property access will fail. | |
if (typeof(error) != "object") { | |
return; | |
} | |
// Push an error on the stack. | |
errors.push(error); | |
} | |
/** | |
* Catch uncaught exceptions generated by JavaScript code. | |
*/ | |
window.onerror = function(message, url, line, column, error) { | |
addError({ | |
type: "javascript", | |
message: message, | |
location: url + ":" + line + ":" + column | |
}); | |
}; | |
/** | |
* Handle uncaught AJAX exceptions that are invoked via jQuery AJAX methods. | |
*/ | |
$(document).ajaxError(function(event, xhr, settings, error) { | |
addError({ | |
type: "xhr", | |
url: settings.url, | |
method: settings.type, | |
message: error, | |
statusCode: xhr.status, | |
response: xhr.responseJSON ? xhr.responseText : null | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Behat\Behat\Hook\Scope\AfterStepScope; | |
use Behat\MinkExtension\Context\MinkContext; | |
class FeatureContext extends MinkContext | |
{ | |
/** | |
* Grab the JavaScript errors from the session. Only works in companion | |
* with a global window variable `errors` that contains the JavaScript | |
* and/or XHR errors. | |
* | |
* @AfterStep | |
*/ | |
public function takeJSErrorsAfterFailedStep(AfterStepScope $event) | |
{ | |
$code = $event->getTestResult()->getResultCode(); | |
$driver = $this->getSession()->getDriver(); | |
if ($driver instanceof Selenium2Driver && $code === 99) { | |
// Fetch errors from window variable. | |
try { | |
$json = $this->getSession()->evaluateScript("return JSON.stringify(window.errors);"); | |
} catch (\Exception $e) { | |
// Ignore this exception, because this may be caused by the | |
// driver and/or JavaScript. | |
return; | |
} | |
// Unserialize the errors. | |
$errors = json_decode($json); | |
if (json_last_error() == JSON_ERROR_NONE) { | |
$messages = []; | |
foreach ($errors as $error) { | |
if ($error->type == "javascript") { | |
$messages[] = "- {$error->message} ({$error->location})"; | |
} elseif ($error->type == "xhr") { | |
$messages[] = "- {$error->message} ({$error->method} {$error->url}): {$error->statusCode} {$error->response}"; | |
} | |
} | |
printf("JavaScript errors:\n\n" . implode("\n", $messages)); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
And some error happens | |
Some error happened. | |
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16' | |
System info: host: 'VBox-van-BasilFX', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-28-generic', java.version: '1.8.0_45-internal' | |
Driver info: driver.version: unknown (WebDriver\Exception\UnknownError) | |
│ | |
│ JavaScript errors: | |
│ | |
│ - TypeError: this.getMaxChangeID is not a function (http://localhost:8000/js/index/changelist.js:36:21) | |
│ | |
└─ @AfterStep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much for this.