function DrupalSelenium2Driver::setValue

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver::setValue()
  2. 11.x core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver::setValue()

File

core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php, line 143

Class

DrupalSelenium2Driver
Provides a driver for Selenium testing.

Namespace

Drupal\FunctionalJavascriptTests

Code

public function setValue($xpath, $value) {
  /** @var \Exception $not_clickable_exception */
  $not_clickable_exception = NULL;
  $result = $this->waitFor(10, function () use (&$not_clickable_exception, $xpath, $value) {
    try {
      $element = $this->getWebDriverSession()
        ->element('xpath', $xpath);
      // \Behat\Mink\Driver\Selenium2Driver::setValue() will call .blur() on
      // the element, modify that to trigger the "input" and "change" events
      // instead. They indicate the value has changed, rather than implying
      // user focus changes. This script only runs when Drupal javascript has
      // been loaded.
      $this->executeJsOnElement($element, <<<JS
if (typeof Drupal !== 'undefined') {
  var node = {{ELEMENT}};
  var original = node.blur;
  node.blur = function() {
    node.dispatchEvent(new Event("input", {bubbles:true}));
    node.dispatchEvent(new Event("change", {bubbles:true}));
    // Do not wait for the debounce, which only triggers the 'formUpdated` event
    // up to once every 0.3 seconds. In tests, no humans are typing, hence there
    // is no need to debounce.
    // @see Drupal.behaviors.formUpdated
    node.dispatchEvent(new Event("formUpdated", {bubbles:true}));
    node.blur = original;
  };
}
JS
);
      if (!is_string($value) && strtolower($element->name()) === 'input' && in_array(strtolower($element->attribute('type')), [
        'text',
        'number',
        'radio',
      ], TRUE)) {
        // @todo Trigger deprecation in
        //   https://www.drupal.org/project/drupal/issues/3421105.
        $value = (string) $value;
      }
      parent::setValue($xpath, $value);
      return TRUE;
    } catch (Exception $exception) {
      if (!JSWebAssert::isExceptionNotClickable($exception) && !str_contains($exception->getMessage(), 'invalid element state')) {
        // Rethrow any unexpected exceptions.
        throw $exception;
      }
      $not_clickable_exception = $exception;
      return NULL;
    }
  });
  if ($result !== TRUE) {
    throw $not_clickable_exception;
  }
}

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