Skip to content

Instantly share code, notes, and snippets.

@generalconsensus
Last active March 4, 2016 13:54
Show Gist options
  • Save generalconsensus/34fef1876bae82b980b5 to your computer and use it in GitHub Desktop.
Save generalconsensus/34fef1876bae82b980b5 to your computer and use it in GitHub Desktop.
behat autoselect chosen.js
<?php
use Behat\Mink\Exception\ElementNotFoundException;
/**
* @Given /^I select "([^"]*)" from "([^"]*)" chosen\.js autoselect box$/
*/
public function iSelectFromChosenJAutoselectBox($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');
$javascript = "var select = jQuery('#$id > option:contains(\'$option\')').val();
jQuery('#$id').val(select).change().trigger('chosen:updated');";
$this->getSession()->executeScript($javascript);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment