You are here

AwaitTrait.inc in Lightning Core 8.3

File

tests/contexts/AwaitTrait.inc
View source
<?php

namespace Acquia\LightningExtension\Context;

use Behat\Mink\Exception\UnsupportedDriverActionException;
use Drupal\DrupalExtension\Context\MinkContext;

/**
 * @internal
 *   This is an internal part of Lightning Core's testing system and may be
 *   changed or removed at any time without warning. It should not be extended,
 *   instantiated, or used in any way by external code! If you need to use this
 *   functionality, you should copy the relevant code into your own project.
 */
trait AwaitTrait {

  /**
   * Waits for a JavaScript condition to become true.
   *
   * @param string $expression
   *   The JavaScript expression to wait for.
   * @param int $timeout
   *   (optional) How long, in seconds, to wait before timing out.
   */
  protected function awaitExpression($expression, $timeout = 10) {
    try {
      $this
        ->getSession()
        ->wait($timeout * 1000, $expression);
    } catch (UnsupportedDriverActionException $e) {
      sleep($timeout);
    }
  }

  /**
   * Waits for an element to exist.
   *
   * @param string $selector
   *   The element's CSS selector.
   * @param int $timeout
   *   (optional) How long, in seconds, to wait before timing out.
   *
   * @return \Behat\Mink\Element\NodeElement
   *   The awaited element.
   */
  protected function awaitElement($selector, $timeout = 10) {
    $js = 'document.querySelector("' . addslashes($selector) . '")';
    $this
      ->awaitExpression($js, $timeout);
    return $this
      ->assertSession()
      ->elementExists('css', $selector);
  }

  /**
   * Waits for AJAX to finish.
   *
   * If the Mink context is unavailable, or the current driver does not support
   * waiting for a JavaScript condition, waits $timeout seconds and returns.
   *
   * @param int $timeout
   *   (optional) How many seconds to wait.
   */
  protected function awaitAjax($timeout = 10) {

    /** @var \Drupal\DrupalExtension\Context\MinkContext $context */
    $context = $this
      ->getContext(MinkContext::class);
    if ($context) {
      try {
        return $context
          ->iWaitForAjaxToFinish();
      } catch (UnsupportedDriverActionException $e) {

        // Fall through to sleep().
      }
    }
    sleep($timeout);
  }

}

Traits

Namesort descending Description
AwaitTrait @internal This is an internal part of Lightning Core's testing system and may be changed or removed at any time without warning. It should not be extended, instantiated, or used in any way by external code! If you need to use this functionality,…