You are here

public function FeatureContext::fillAutocompleteField in Open Social 8.2

Fill multiple autocomplete field.

Parameters

string $field: The field identifier.

string $text: The typed text in field.

string $item: The item for drop-down list.

bool $next: (optional) TRUE if it is not first value.

2 calls to FeatureContext::fillAutocompleteField()
FeatureContext::iFillInWithAndSelect in tests/behat/features/bootstrap/FeatureContext.php
@Given I fill in :field with :text and select :item
FeatureContext::iFillNextInWithAndSelect in tests/behat/features/bootstrap/FeatureContext.php
@Given I fill next in :field with :text and select :item

File

tests/behat/features/bootstrap/FeatureContext.php, line 927

Class

FeatureContext
Defines application features from the specific context.

Code

public function fillAutocompleteField($field, $text, $item, $next = FALSE) {
  $element = $this
    ->getSession()
    ->getPage()
    ->findField($field);
  if (null === $element) {
    throw new \Exception(sprintf('Field %s not found', $field));
  }
  if ($next) {
    $text = $element
      ->getValue() . ', ' . $text;
  }
  $element
    ->setValue($text);
  $element
    ->keyDown(' ');
  sleep(1);

  // Wait timeout before sending an AJAX request.
  $this->minkContext
    ->iWaitForAjaxToFinish();
  $id = $element
    ->getAttribute('id');
  $index = $this
    ->getSession()
    ->evaluateScript('return jQuery(".ui-autocomplete-input").index(jQuery("#' . $id . '"));');
  $autocomplete = $this
    ->getSession()
    ->getPage()
    ->find('xpath', '//ul[contains(@class, "ui-autocomplete")][' . ($index + 1) . ']');
  if (null === $autocomplete) {
    throw new \Exception('Could not find the autocomplete popup box');
  }
  $popup_element = $autocomplete
    ->find('xpath', "//li[text()='{$item}']");

  // If "li" was not found, try to find "a" inside a "li".
  if (null === $popup_element) {
    $popup_element = $autocomplete
      ->find('xpath', "//li/a[text()='{$item}']");
  }

  // If "li" was not found, try to find "div" inside a "li".
  if (null === $popup_element) {
    $popup_element = $autocomplete
      ->find('xpath', "//li/div[text()='{$item}']");
  }
  if (null === $popup_element) {
    throw new \Exception(sprintf('Could not find autocomplete item with text %s', $item));
  }
  if (!empty($popup_element_id = $popup_element
    ->getAttribute('id'))) {
    $this
      ->getSession()
      ->evaluateScript('jQuery("#' . $popup_element_id . '").click();');
  }
  else {
    $popup_element
      ->click();
  }
  if ($next) {
    $this
      ->getSession()
      ->evaluateScript('jQuery("#' . $id . '").val(jQuery("#' . $id . '").val().replace(/\\s(\\d+\\)\\,\\s)/g, " ($1"));');
  }
}