Drupal Functional tests and Functional Javascript tests automatically produce HTML output every time the test makes a page request.

This in my test code:

    $this->drupalGet('some/path');

gets me this once my test has run:

HTML output was generated
http://my-host/sites/simpletest/browser_output/Drupal_Tests_yadayada-91-80728919.html

This is great! You can navigate between the pages, see exactly what the test code saw, inspect the HTML, and so on.

However, there's no such output when a Functional Javascript test causes an AJAX request.

It's fairly simple to create the debug output manually in the test code, and it will be added to the list of output HTML pages:

// Get the Mink page element.
$page = $this->getSession()->getPage();

// Change a form element that will react with AJAX, and wait for the request to
// complete.
$page->selectFieldOption('my-field', '1');
$this->assertSession()->assertWaitOnAjaxRequest();

// Output the new HTML.
$this->htmlOutput($page->getHtml());

It would be really good though if this happened automatically!

AFAIK, we intercept the normal page requests to save the HTML here:

  /**
   * Provides a Guzzle middleware handler to log every response received.
   *
   * @return callable
   *   The callable handler that will do the logging.
   */
  protected function getResponseLogHandler() {

so we'd need a totally different way to hook into Mink.

Comments

joachim created an issue. See original summary.

joachim’s picture

The title of this issue seems to suggest it fixed this a long time ago, but if it did, it doesn't work any more: #2763401: PHPunit browser tests should log all Mink requests

jibran’s picture

Category: Feature request » Task
Priority: Major » Normal

This makes sense to me and thank you for creating the issue. This seems like a task to me but not a major one. Feel free to disagree.

joachim’s picture

Been poking around in Mink and its dependencies to see where we could hook in...

instaclick/php-webdriver is the package that appears to be doing the actual talking to the browser. That has a swappable service for its curl handling.

So in a test class's setUp(), we can do this:

    \WebDriver\ServiceFactory::getInstance()->setServiceClass('service.curl', DrupalCurlService::class);

and then provide this class:

use WebDriver\Service\CurlService;

class DrupalCurlService extends CurlService {

  public function execute($requestMethod, $url, $parameters = null, $extraOptions = array()) {
    dump($requestMethod);
    dump($url);

    return parent::execute($requestMethod, $url, $parameters, $extraOptions);
  }

}

and that dumps a TON of requests. That's far too many (my test only made a couple of AJAX requests), so I don't know how we'd filter that down to the ones we care about.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.