Skip to content

Commit

Permalink
Allow to pass array for select multiple. (#904)
Browse files Browse the repository at this point in the history
* Allow to pass array for select multiple.

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Update InteractsWithElements.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
crynobone and taylorotwell authored Jul 2, 2021
1 parent 8cab2c6 commit 8710ca0
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Concerns/InteractsWithElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverKeys;
use Facebook\WebDriver\WebDriverSelect;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait InteractsWithElements
Expand Down Expand Up @@ -207,7 +209,7 @@ public function clear($field)
* Select the given value or random value of a drop-down field.
*
* @param string $field
* @param string $value
* @param string|array|null $value
* @return $this
*/
public function select($field, $value = null)
Expand All @@ -216,18 +218,34 @@ public function select($field, $value = null)

$options = $element->findElements(WebDriverBy::cssSelector('option:not([disabled])'));

$select = $element->getTagName() === 'select' ? new WebDriverSelect($element) : null;

$isMultiple = false;

if (! is_null($select)) {
if ($isMultiple = $select->isMultiple()) {
$select->deselectAll();
}
}

if (func_num_args() === 1) {
$options[array_rand($options)]->click();
} else {
if (is_bool($value)) {
$value = $value ? '1' : '0';
}
$value = collect(Arr::wrap($value))->transform(function ($value) {
if (is_bool($value)) {
return $value ? '1' : '0';
}

return (string) $value;
})->all();

foreach ($options as $option) {
if ((string) $option->getAttribute('value') === (string) $value) {
if (in_array((string) $option->getAttribute('value'), $value)) {
$option->click();

break;
if (! $isMultiple) {
break;
}
}
}
}
Expand Down

0 comments on commit 8710ca0

Please sign in to comment.