Completed
Push — 1.0.x ( 919118...91e29f )
by Fabian
06:07
created

AutocompleteContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B fillInDrupalAutocomplete() 0 37 4
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use Behat\Mink\Exception\UnsupportedDriverActionException;
7
use NuvoleWeb\Drupal\DrupalExtension\Context\RawMinkContext;
8
9
/**
10
 * Autocomplete step definitions.
11
 */
12
class AutocompleteContext extends RawMinkContext {
13
14
  /**
15
   * Fill out autocomplete fields based on gist.
16
   *
17
   * @When I fill in the autocomplete :autocomplete with :text
18
   */
19
  public function fillInDrupalAutocomplete($locator, $text) {
20
21
    $session = $this->getSession();
22
    $el = $session->getPage()->findField($locator);
23
24
    if (empty($el)) {
25
      throw new ExpectationException('No such autocomplete element ' . $locator, $session);
26
    }
27
28
    // Set the text and trigger the autocomplete with a space keystroke.
29
    $el->setValue($text);
30
31
    try {
32
      $el->keyDown(' ');
33
      $el->keyUp(' ');
34
35
      // Wait for ajax.
36
      $this->getSession()->wait(1000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
37
      // Wait a second, just to be sure.
38
      sleep(1);
39
40
      // Select the autocomplete popup with the name we are looking for.
41
      $popup = $session->getPage()->find('xpath', "//ul[contains(@class, 'ui-autocomplete')]/li/a[text() = '{$text}']");
42
43
      if (empty($popup)) {
44
        throw new ExpectationException('No such option ' . $text . ' in ' . $locator, $session);
45
      }
46
47
      // Clicking on the popup fills the autocomplete properly.
48
      $popup->click();
49
    }
50
    catch (UnsupportedDriverActionException $e) {
51
      // So javascript is not supported.
52
      // We did set the value correctly, so Drupal will figure it out.
53
    }
54
55
  }
56
57
}
58