Skip to content

Instantly share code, notes, and snippets.

@ratajczak
Created November 16, 2015 10:20
Show Gist options
  • Save ratajczak/bb19f1773f3c6e8e96b6 to your computer and use it in GitHub Desktop.
Save ratajczak/bb19f1773f3c6e8e96b6 to your computer and use it in GitHub Desktop.
/**
* @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->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);
}
@generalconsensus
Copy link

@ratajczak Super helpful!

@generalconsensus
Copy link

@ratajczak I updated your example to fix the Exception handling here: https://gist.github.com/generalconsensus/eff13d8d572327024baf

@shwetaneelsharma
Copy link

Thank you so much. Works like a charm even in a headless environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment