Skip to content

Instantly share code, notes, and snippets.

@generalconsensus
Forked from ratajczak/behat select chosen.js
Last active September 5, 2017 23:16
Show Gist options
  • Save generalconsensus/eff13d8d572327024baf to your computer and use it in GitHub Desktop.
Save generalconsensus/eff13d8d572327024baf to your computer and use it in GitHub Desktop.
use Behat\Mink\Exception\ElementNotFoundException;
/**
* @Given /^I select "([^"]*)" from "([^"]*)" chosen\.js select box$/
*/
public function iSelectFromChosenJsSelectBox($option, $select) {
$select = $this->fixStepArgument($select);
$option = $this->fixStepArgument($option);
$page = $this->getSession()->getPage();
$field = $page->findField($select, true);
if (null === $field) {
throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id|name|label|value', $select);
}
$id = $field->getAttribute('id');
$opt = $field->find('named', array('option', $option));
$val = $opt->getValue();
$javascript = "jQuery('#$id').val('$val');
jQuery('#$id').trigger('chosen:updated');
jQuery('#$id').trigger('change');";
$this->getSession()->executeScript($javascript);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment