Skip to content

Instantly share code, notes, and snippets.

@steffenr
Last active September 13, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steffenr/c3e79cc0d4dec8defe5cee7406274087 to your computer and use it in GitHub Desktop.
Save steffenr/c3e79cc0d4dec8defe5cee7406274087 to your computer and use it in GitHub Desktop.
Behat / Select2 Feature context
<?php
/**
* @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
*/
public function iFillInSelectInputWithAndSelect($field, $value, $entry)
{
$page = $this->getSession()->getPage();
$inputField = $page->find('css', $field);
if (!$inputField) {
throw new \Exception('No field found');
}
$choice = $inputField->getParent()->find('css', '.select2-selection');
if (!$choice) {
throw new \Exception('No select2 choice found');
}
$choice->press();
$select2Input = $page->find('css', '.select2-search__field');
if (!$select2Input) {
throw new \Exception('No input found');
}
$select2Input->setValue($value);
$this->getSession()->wait(1000);
$chosenResults = $page->findAll('css', '.select2-results li');
foreach ($chosenResults as $result) {
if ($result->getText() == $entry) {
$result->click();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment