Last active
September 13, 2018 12:24
-
-
Save steffenr/c3e79cc0d4dec8defe5cee7406274087 to your computer and use it in GitHub Desktop.
Behat / Select2 Feature context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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