public function TestSubContext::iSelectFirstAutocomplete in Panopoly 8.2
Same name and namespace in other branches
- 7 modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc \TestSubContext::iSelectFirstAutocomplete()
Selects the first autocomplete option.
@When I select the first autocomplete option for :text on the :field field
File
- modules/
panopoly/ panopoly_test/ behat/ steps/ panopoly_test.behat.inc, line 1001 - Provide Behat step-definitions for generic Panopoly tests.
Class
- TestSubContext
- Behat sub-context for Panopoly.
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();
}