calebporzio's avatar

Testing Multiple Select

To test a select box, I would typically do this:

$this->select('value', 'inputName');

How would one test a multiple select with an array of inputs? Something like:

$this->select(['value1', 'value2', 'value3'], 'inputName[]');

I know some others have asked similar questions on here but have come up dry, any ideas?

0 likes
4 replies
llhilton's avatar

Have you been able to figure this one out? I just ran into it today. I tried even just

$this->select('value 1', 'inputName[]');

But that gives me the error InvalidArgumentException: Unreachable field "" (Which is the same error I get with the array method, as I just found out.)

From what I've been able to tell, it looks like this may be because the code under the hood is using a string instead of an array? (At least, that's the problem if using the Integrated package, which the Laravel tests took a big chunk of, I think?)

PaYLuZ's avatar

I made a workaround on this.

 /**
     * Select the given value or random value of a drop-down field.
     *
     * @param  string $field
     * @param $browser
     * @param array|null $values
     * @return $this
     * @internal param string $value
     */
    public function selectMultiple($field, $browser, $values = [])
    {
        $element = $browser->resolver->resolveForSelection($field);

        $options = $element->findElements(WebDriverBy::tagName('option'));

        if (empty($values)) {
            $maxSelectValues = sizeof($options) - 1;
            $minSelectValues = rand(0, $maxSelectValues);
            foreach (range($minSelectValues, $maxSelectValues) as $optValue) {
                $options[$optValue]->click();
            }
        } else {
            foreach ($options as $option) {
                $optValue = (string)$option->getAttribute('value');
                if (in_array($optValue, $values)) {
                    $option->click();
                }
            }
        }

        return $this;
    }

Then you will call the function like this.

$this->browse(function (Browser $browser) {
    $this->selectMultiple("field_name[]", $browser)
}

Or you might do this if don't want to randomized the selected values

$this->browse(function (Browser $browser) {
    $this->selectMultiple("field_name[]", $browser, ["value1", "value2"])
}

I hope it helps.

1 like
eugenefvdm's avatar

Hey @calebporzio ,

It's been five years since you've posted this, any clues how to do this:

->select('suburb[]', [641,4500])

Does Selenium allow select multiple in testing?

calebporzio's avatar
calebporzio
OP
Best Answer
Level 7

Hey @eugenevdm - it looks like this behavior was just added to Laravel Dusk. If you update your Dusk to the latest version, you should be able to pass an array as the second param to select multiple.

Please or to participate in this conversation.