You are here

public function JSWebAssert::assertWaitOnAjaxRequest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest()

Waits for AJAX request to be completed.

Parameters

int $timeout: (Optional) Timeout in milliseconds, defaults to 10000.

string $message: (optional) A message for exception.

Throws

\RuntimeException When the request is not completed. If left blank, a default message will be displayed.

File

core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php, line 30

Class

JSWebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\FunctionalJavascriptTests

Code

public function assertWaitOnAjaxRequest($timeout = 10000, $message = 'Unable to complete AJAX request.') {

  // Wait for a very short time to allow page state to update after clicking.
  usleep(5000);
  $condition = <<<JS
      (function() {
        function isAjaxing(instance) {
          return instance && instance.ajaxing === true;
        }
        return (
          // Assert no AJAX request is running (via jQuery or Drupal) and no
          // animation is running.
          (typeof jQuery === 'undefined' || (jQuery.active === 0 && jQuery(':animated').length === 0)) &&
          (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
        );
      }())
JS;
  $result = $this->session
    ->wait($timeout, $condition);
  if (!$result) {
    throw new \RuntimeException($message);
  }
}