Skip to content

Commit

Permalink
Merge pull request #10 from andrewmy/multiple_element_search
Browse files Browse the repository at this point in the history
Multiple element search + optgroup fix
  • Loading branch information
roquie authored May 16, 2018
2 parents 100d251 + fbb9455 commit c9a125a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

use Laravel\Dusk\Browser;
use Facebook\WebDriver\Exception\ElementNotVisibleException;

/**
* Register simple macros for the Laravel Dusk.
Expand Down Expand Up @@ -35,17 +36,22 @@

// check if search field exists and fill it.
if ($element = $this->element('.select2-container.select2-container--open .select2-search__field')) {
foreach ((array) $value as $item) {
$element->sendKeys($item);
sleep($wait);
$this->click('.select2-results__option--highlighted');
try {
foreach ((array) $value as $item) {
$element->sendKeys($item);
sleep($wait);
$this->click('.select2-results__option--highlighted');
}

return $this;
} catch (ElementNotVisibleException $exception) {
// ignore the exception and try another way
}

return $this;
}

// another way - w/o search field.
$this->script("jQuery.find(\".select2-results__options .select2-results__option:contains('{$value}')\")[0].click()");
$field = \str_replace('\\', '\\\\', $field);
$this->script("jQuery(\"$field\").val((function () { return jQuery(\"$field option:contains('$value')\").val(); })()).trigger('change')");

return $this;
});
8 changes: 5 additions & 3 deletions tests/Browser/Select2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public function testShouldBeSelect2SelectableAutomatically()
$browser
->visit('/')
->select2('.js-example-basic-single', 'Calif')
->assertSee('California')
->select2('.js-example-basic-hide-search')
->assertSeeIn('.js-example-basic-single + .select2', 'California')
->select2('.js-example-basic-hide-search', 'Nev')
->assertSeeIn('.js-example-basic-hide-search + .select2', 'Nevada')
->click('h3:first-child') // close the previous select
->select2('.js-example-basic-multiple', 'Wy')
->waitFor('.select2-selection__choice')
->select2('.js-example-basic-multiple')
->select2('.js-data-example-ajax', 'laravel')
->assertSee('laravel')
->waitForText('laravel')
;
});
}
Expand Down

0 comments on commit c9a125a

Please sign in to comment.