Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass array for select multiple. #904

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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