Skip to content

Allow to set option without value #673

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Symfony\Component\Panther\DomCrawler\Field;

use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\WebDriverSelect;
use Facebook\WebDriver\WebDriverSelectInterface;
use Symfony\Component\DomCrawler\Field\ChoiceFormField as BaseChoiceFormField;
Expand Down Expand Up @@ -42,7 +43,11 @@ public function hasValue(): bool
public function select($value): void
{
foreach ((array) $value as $v) {
$this->selector->selectByValue($v);
try {
$this->selector->selectByValue($v);
} catch (NoSuchElementException) {
$this->selector->selectByVisibleText($v);
}
}
}

Expand Down Expand Up @@ -130,9 +135,7 @@ public function setValue($value): void
return;
}

foreach ((array) $value as $v) {
$this->selector->selectByValue($v);
}
$this->select($value);
}

public function addChoice(\DOMElement $node): void
Expand Down
34 changes: 34 additions & 0 deletions tests/DomCrawler/Field/ChoiceFormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@ public function testGetValueFromSelectMultipleIfNoneIsSelected(callable $clientF
$this->assertSame([], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testGetValueFromSelectSingleWithoutValue(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

/** @var ChoiceFormField $field */
$field = $form['select_single_without_value'];
$this->assertInstanceOf(ChoiceFormField::class, $field);
$this->assertSame('none selected', $field->getValue());

$field->select('thirty');
$this->assertEquals('thirty', $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testGetValueFromSelectMultipleWithoutValue(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

/** @var ChoiceFormField $field */
$field = $form['select_multiple_without_value'];
$this->assertInstanceOf(ChoiceFormField::class, $field);
$this->assertSame([], $field->getValue());

$field->select('thirty');
$this->assertEquals(['thirty'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/choice-form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@
</select>
</label>

<label class="field">
select_single_without_value
<select name="select_single_without_value">
<option>none selected</option>
<option id="single_without_20">twenty</option>
<option id="single_without_30">thirty</option>
</select>
</label>

<label class="field">
select_multiple_without_value
<select name="select_multiple_without_value" multiple="multiple">
<option>none selected</option>
<option id="multiple_without_20">twenty</option>
<option id="multiple_without_30">thirty</option>
</select>
</label>

<div class="field">
radio_checked
<span><input type="radio" name="radio_checked" value="i_am_not_checked" />i_am_not_checked</span>
Expand Down
Loading