You are here

public function TestSubContext::iSelectFirstAutocomplete in Drupal Commons 7.3

@When I select the first autocomplete option for :text on the :field field

File

tests/steps/commons_test.behat.inc, line 627
Provide Behat step-definitions for generic Commons tests.

Class

TestSubContext

Code

public function iSelectFirstAutocomplete($text, $field) {
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $element = $page
    ->findField($field);
  if (empty($element)) {
    throw new \Exception(sprintf('We couldn\'t find "%s" on the page', $field));
  }
  $page
    ->fillField($field, $text);
  $xpath = $element
    ->getXpath();
  $driver = $session
    ->getDriver();

  // autocomplete.js uses key down/up events directly.
  // Press the backspace key.
  $driver
    ->keyDown($xpath, 8);
  $driver
    ->keyUp($xpath, 8);

  // Retype the last character.
  $chars = str_split($text);
  $last_char = array_pop($chars);
  $driver
    ->keyDown($xpath, $last_char);
  $driver
    ->keyUp($xpath, $last_char);

  // Wait for AJAX to finish.
  $this
    ->iWaitForAJAX();

  // And make sure the autocomplete is showing.
  $this
    ->getSession()
    ->wait(5000, 'jQuery("#autocomplete").show().length > 0');

  // And wait for 1 second just to be sure.
  sleep(1);

  // Press the down arrow to select the first option.
  $driver
    ->keyDown($xpath, 40);
  $driver
    ->keyUp($xpath, 40);

  // Press the Enter key to confirm selection, copying the value into the field.
  $driver
    ->keyDown($xpath, 13);
  $driver
    ->keyUp($xpath, 13);

  // Wait for AJAX to finish.
  $this
    ->iWaitForAJAX();
}