Last active
March 2, 2017 15:45
-
-
Save choult/52176db29472607f40269c2431b6c855 to your computer and use it in GitHub Desktop.
Selecting from a selectize.js styled dropdown in Behat
This file contains hidden or 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 | |
/** | |
* {@inheritdoc} | |
*/ | |
public function selectOption($select, $option) | |
{ | |
$session = $this->getSession(); | |
$page = $session->getPage(); | |
$field = $page->findField($select); | |
if (!$field) { | |
throw new \RuntimeException("Could not find field '{$select}'"); | |
} | |
if ($field->hasClass('selectized')) { | |
return $this->selectFromSelectize($option, $select, $field); | |
} | |
return parent::selectOption($select, $option); | |
} | |
/** | |
* Selects the passed option from a selectize.js dropdown | |
* | |
* @param string $option | |
* @param string $selector | |
* @param \Behat\Mink\Element\NodeElement $field | |
* | |
* @throws \RuntimeException | |
*/ | |
private function selectFromSelectize($option, $selector, NodeElement $field) | |
{ | |
$parent = $field->getParent(); | |
$element = $parent->find('css', '.selectize-input input'); | |
if (!$element) { | |
throw new \RuntimeException("Could not find selectize element for '{$selector}'"); | |
} | |
$element->click(); | |
$elements = $parent->findAll('css', '.selectize-dropdown-content .option'); | |
foreach ($elements as $element) { | |
if ($element->getText() == $option) { | |
$element->click(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment