Drupal 10: Printing Page Content In Browser Unit Tests

If you need to view the contents of a page when running a \Drupal\Tests\BrowserTestBase test then you can use the following technique.

/**
 * Test that a user can access their own referral tab.
 */
public function testUserCanAccessTheirOwnReferralTab() {
  $user = $this->drupalCreateUser();
  $this->drupalLogin($user);

  $this->drupalGet('user/' . $user->id());

  fwrite(STDERR, var_export($this->getSession()->getPage()->getContent(), TRUE));

  // Continue testing here.
}

This will render the current session page content to the command line, which is useful if you are unable to view the output of the test run. This might be the case when running tests in a continuous integration environment.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 15 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.