@ludo237 You might be experiencing issues because you’re trying to select a disabled option. I imagine this is invalid in HTML.
Testing <select> with default option
Hello everyone.
I'm writing a test for a view and I'm using native phpunit + laravel test suite without any third parties packages because I think that they suit my needs.
I've only one question tho, which is basically written in the title, how do I test a <select> tag with an <option disabled selected="true"/>?
This is the HTML inside the blade.
<select name="from[hour]" id="fromHour" class="form-control">
<option disabled aria-disabled="true" selected aria-selected="true">Hour</option>
@for($i = 1; $i <= 24; ++$i)
<option value="{{ $i }}" {{ (old("from.hour") == $i) ? "selected" : "" }}>{{ $i }}</option>
@endfor
</select>
Don't look at the styling, it's just a dummy select. Now my test suite is using the classic fillForm + select($value, $name) method combo to fill the entire form the problem is that if I leave the <option selected/> element, the test will fail because it will stick with the default option but if I remove that <option selected /> the test will chose a random number from the <select> element which is fine and intended.
Here's the code involved
$dumpResult = $this->visit("foo/bar")
->seeText("Save Form")
->select($randomNumber, "from[hour]")
->fillForm("Save Form", [
"name" => Factory::create()->firstName,
"surname" => Factory::create()->lastName,
]);
If I dd the result of dumpResult it will shows me any random number within the range, which is intended, because there isn't any default option but that is not the case when there is a default option.
I need that default option, and I cannot change the whole application only because the test suite is unable to fetch the <select> element. What can I do to solve this issue?
Related SO Question (if you want free Karma): http://stackoverflow.com/questions/35888161/laravelphpunit-testing-select-with-default-option
Thanks.
Please or to participate in this conversation.