Skip to main content

Failure diagnostics

DrevOps\BehatSteps\Steps\Web\DiagnosticsTrait - Source, Example, Helpers

Append on-failure diagnostics to the failure message of any failed step.

The exception message of a failed step is often not enough to diagnose a CI failure. This trait hooks every step and, only when the step failed, appends a compact diagnostics block to the failure message:

  • URL - the current page URL.
  • HTTP status - the last response status code.
  • Browser driver - the class of the browser driver behind the session.
  • JS console errors - collected JavaScript errors, when a JavaScript-capable browser driver is active and errors were captured. Reads the buffer kept by JavascriptTrait when the context also uses it, and the live browser buffer.
  • Re-run - a ready-to-paste command that re-runs just the failing scenario.

The trait is active with no further configuration once a context composes it. The enabled option switches it off.

Every field is individually toggleable through its show_* option or by overriding its diagnosticsGetShow*() method to return FALSE. Each value source degrades to nothing when the browser driver cannot provide it, so a failed step is never turned into a different failure.

Skip processing with tags: @behat-steps-skip:DiagnosticsTrait.

Scenario: A failing step prints diagnostics
Given I am on "/some-page"
Then I should see "text that is not there"
# On failure the message gains:
# --- Failure diagnostics ---
# URL: http://example.com/some-page
# HTTP status: 200
# Browser driver: Behat\Mink\Driver\BrowserKitDriver
# Re-run: vendor/bin/behat features/example.feature:3

Options​

OptionTypeDefaultTagDescription
diagnostics.enabledbooleanTRUE@behat-steps-skip:DiagnosticsTraitAppend a diagnostics block to the message of a failing step.
diagnostics.headerstring'--- Failure diagnostics ---'-Header line that precedes the diagnostics block.
diagnostics.rerun_binarystring'vendor/bin/behat'-Binary named in the re-run command the block prints.
diagnostics.show_urlbooleanTRUE-Include the current URL in the block.
diagnostics.show_status_codebooleanTRUE-Include the HTTP status code in the block.
diagnostics.show_browser_driverbooleanTRUE-Include the browser driver class in the block.
diagnostics.show_js_errorsbooleanTRUE-Include the JavaScript console errors in the block.
diagnostics.show_rerunbooleanTRUE-Include the command that re-runs the failing scenario in the block.