From 8710ca063e4de0f09a18b517ed8cbb41455fcebe Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 3 Jul 2021 00:37:17 +0800 Subject: [PATCH] Allow to pass array for select multiple. (#904) * Allow to pass array for select multiple. Signed-off-by: Mior Muhammad Zaki * Apply fixes from StyleCI Signed-off-by: Mior Muhammad Zaki * Update InteractsWithElements.php Co-authored-by: Taylor Otwell --- src/Concerns/InteractsWithElements.php | 30 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Concerns/InteractsWithElements.php b/src/Concerns/InteractsWithElements.php index 6cb5f39b5..ca687248d 100644 --- a/src/Concerns/InteractsWithElements.php +++ b/src/Concerns/InteractsWithElements.php @@ -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 @@ -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) @@ -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; + } } } }