Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created February 6, 2023 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WengerK/56ca7b3f6024b0a61a26fdc73c8c3f13 to your computer and use it in GitHub Desktop.
Save WengerK/56ca7b3f6024b0a61a26fdc73c8c3f13 to your computer and use it in GitHub Desktop.
Debug Symfony - Functional Tests & Panther Tests

Debug Symfony - Functional Tests & Panther Tests

Debugging tests can often be a pain, but using those snippets it's possible to get a lot of informations when using WebTestCase or PantherTestCase.

Get HTML of a page

Sometimes you'll want to inspect the HTML of the page or a specific element. There is no official method for this yet but the following works:

$crawler = $client->request('GET', '/my-url/');
file_put_contents('public/debug.html', $client->getResponse());

Your test will now fail with an HTML page generated and accessible on http//app.test/debug.html.

Get Screenshot of a page (PantherTestCase)

If you want to see a screenshot to work out what is going on when your tests fails

/**
 * @AfterStep
 */
public function takeScreenshotOnFailur(AfterStepScope $event) {
  if ($event->getTestResult()->isPassed) {
    return;
  }
  
  file_put_contents('public/debug.html', self::$pantherClient->getPageSource());
  self::$patherClient->takeScreenshot('public/debug.png');
}

Your test will now fail with an HTML page generated and accessible on http//app.test/debug.html and http//app.test/debug.png.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment