3 namespace Drupal\Tests\Listeners;
6 * Defines a class for providing html output results for functional tests.
10 trait HtmlOutputPrinterTrait {
13 * File to write html links to.
17 protected $browserOutputFile;
20 * Creates the file to list the HTML output created during the test.
22 * @see \Drupal\Tests\BrowserTestBase::initBrowserOutputFile()
24 protected function setUpHtmlOutput() {
25 if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) {
26 // Initialize html output debugging.
27 $html_output_directory = rtrim($html_output_directory, '/');
29 // Check if directory exists.
30 if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) {
31 $this->writeWithColor('bg-red, fg-black', "HTML output directory $html_output_directory is not a writable directory.");
34 // Convert to a canonicalized absolute pathname just in case the current
35 // working directory is changed.
36 $html_output_directory = realpath($html_output_directory);
37 $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_');
38 if ($this->browserOutputFile) {
39 touch($this->browserOutputFile);
42 $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory.");
47 if ($this->browserOutputFile) {
48 putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
51 // Remove any environment variable.
52 putenv('BROWSERTEST_OUTPUT_FILE');
57 * Prints the list of HTML output generated during the test.
59 protected function printHtmlOutput() {
60 if ($this->browserOutputFile) {
61 $contents = file_get_contents($this->browserOutputFile);
63 $this->writeNewLine();
64 $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
65 $this->write($contents);
67 // No need to keep the file around any more.
68 unlink($this->browserOutputFile);