Debugging tests can often be a pain, but using those snippets it's possible to get a lot of informations when using BrowserTestBase
or JavascriptTestBase
.
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:
$this->assertEquals('', $this->getSession()->getPage()->getHTML());
Your test will now fail with the HTML in the assert error message.
Sometimes, the HTMl page is way too heavy to be printed on the console. You can then just print it to a file
file_put_contents('/PATH/TO/test.html', $this->getSession()->getPage()->getHTML());
Don't put the screenshot in a path inside the test environment like public://test.html as this will be cleaned up at the end of the test.
$this->createScreenshot(\Drupal::root() . '/sites/default/files/simpletest/test.html');
If you want to see a screenshot to work out what is going on you can do this! (introduced in 8.1.9)
$this->createScreenshot('PATH/TO/screenshot.png');
Don't put the screenshot in a path inside the test environment like public://test.jpg as this will be cleaned up at the end of the test.
$this->createScreenshot(\Drupal::root() . '/sites/default/files/simpletest/screen.png');
If your test is having a strange problem, you can add a line like this just before the failure, which will basically stop the test in Chrome and let you play with the test site and look around:
$this->assertSession()->waitForElementVisible('css', '.test-wait', 100000000000000000000000000);
Very very helpful, @WengerK! :)
Thank you!
Just found a Gist with further helpful Drupal testing snippets:
https://gist.github.com/joshsedl/083064e4ec8f5f7cdf0c0d74d423850f