function JSWebAssert::assertWaitOnAjaxRequest

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest()
  2. 10 core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest()
  3. 11.x 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 35

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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.