Using jQuery Select2 with Codeception

KC Müller
Aug 9, 2017 · 1 min read

It took me some time to figure out how to fill out a jQuery Select2 dropdown during acceptance testing. The search box is not connected to the input field but appended to the DOM. This will do the trick:

/**
* Fill out select2 option field
*
* @param \Codeception\Actor $I
* @param string $selector
* @param string $value
*
* @return void
*/
public function fillOutSelect2OptionField(Actor $I, $selector, $value)
{
$element = '#select2-' . $selector . '-container';
$I->click($element); $searchField = '.select2-search__field'; $I->waitForElementVisible($searchField); $I->fillField($searchField, $value); $I->pressKey($searchField, WebDriverKeys::ENTER);
}

KC Müller

Written by

Web Developer in Berlin — www.kcmueller.de

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade