diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php index afbf6c3a11..b02e7075c8 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php @@ -20,22 +20,6 @@ */ class JSWebAssert extends WebAssert { - /** - * Opaque hash for the current page, used for tracking per-page AJAX requests. - * - * @var null|string - */ - protected $currentPageHash = NULL; - - /** - * Resets the AJAX state for tracking per-page AJAX requests. - * - * @todo Refactor when https://github.com/instaclick/php-webdriver/issues/54 lands. - */ - public function resetAjaxRequestStateTracking(): void { - $this->currentPageHash = NULL; - } - /** * Waits for AJAX request to be completed. * @@ -50,10 +34,10 @@ public function resetAjaxRequestStateTracking(): void { * @throws \RuntimeException * When the request is not completed. If left blank, a default message will * be displayed. + * @throws \LogicException + * When an unnecessary invocation to this method is detected. */ public function assertWaitOnAjaxRequest($timeout = 10000, $message = 'Unable to complete AJAX request.'): int { - static $ajax_response_count; - // Wait for a very short time to allow page state to update after clicking. usleep(5000); $condition = <<session->evaluateScript(<< entry.initiatorType === 'xmlhttprequest') - .length + return [ + window.drupalCumulativeXhrCount, + window.performance + .getEntries() + .filter(entry => entry.initiatorType === 'xmlhttprequest') + .length + ]; })() -JS; - $current_count = $this->session->evaluateScript($javascript); - - // First invocation on this page: initialization. - // @todo Refactor when https://github.com/instaclick/php-webdriver/issues/54 lands. - $page_hash = $this->session->getCurrentUrl(); - if ($this->currentPageHash !== $page_hash) { - $this->currentPageHash = $page_hash; - $ajax_response_count = NULL; - } +JS); - // Detect unnecessary waits. - if ($current_count === $ajax_response_count) { + // Detect unnecessary AJAX request waits and inform the test author. + if ($drupal_ajax_request_count !== $browser_xhr_request_count) { throw new \LogicException(sprintf('Unnecessary %s call in a test!', __METHOD__)); } - // Update the static variable for the next invocation. - $ajax_response_count = $current_count; - - return $ajax_response_count; + return $drupal_ajax_request_count; } /** diff --git a/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php index 517ff6e8ac..8348b76f28 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php @@ -38,13 +38,6 @@ abstract class WebDriverTestBase extends BrowserTestBase { */ protected $minkDefaultDriverClass = DrupalSelenium2Driver::class; - /** - * The WebDriver assert instance. - * - * @var \Drupal\FunctionalJavascriptTests\WebDriverWebAssert - */ - protected $assertInstance; - /** * {@inheritdoc} */ @@ -201,22 +194,7 @@ protected function createScreenshot($filename, $set_background_color = TRUE) { * {@inheritdoc} */ public function assertSession($name = NULL) { - if (!isset($this->assertInstance)) { - $this->assertInstance = new WebDriverWebAssert($this->getSession($name), $this->baseUrl); - } - return $this->assertInstance; - } - - /** - * {@inheritdoc} - */ - protected function drupalGet($path, array $options = [], array $headers = []) { - $result = parent::drupalGet($path, $options, $headers); - // Ensure that ::assertWaitOnAjaxRequest() can work reliably. - if ($this->assertInstance) { - $this->assertInstance->resetAjaxRequestStateTracking(); - } - return $result; + return new WebDriverWebAssert($this->getSession($name), $this->baseUrl); } /**