Skip to content

Commit 0fa5f65

Browse files
fix: checkbox/radio with value '0' not accessible with byValue() (#627)
Co-authored-by: Kévin Dunglas <[email protected]>
1 parent b3cb34e commit 0fa5f65

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/WebDriver/WebDriverCheckbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private function byVisibleText($text, $partial = false, $select = true): void
231231

232232
private function getRelatedElements($value = null): array
233233
{
234-
$valueSelector = $value ? \sprintf(' and @value = %s', XPathEscaper::escapeQuotes($value)) : '';
234+
$valueSelector = null === $value ? '' : \sprintf(' and @value = %s', XPathEscaper::escapeQuotes($value));
235235
if (null === $formId = $this->element->getAttribute('form')) {
236236
$form = $this->element->findElement(WebDriverBy::xpath('ancestor::form'));
237237
if ('' === $formId = (string) $form->getAttribute('id')) {

tests/WebDriver/WebDriverCheckBoxTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,28 @@ public function testWebDriverCheckboxDeselectByVisiblePartialTextRadio(): void
299299
$c = new WebDriverCheckbox($element);
300300
$c->deselectByVisiblePartialText('AB');
301301
}
302+
303+
/**
304+
* @dataProvider selectByValueDataProviderWithZeroValue
305+
*/
306+
public function testWebDriverCheckboxSelectByValueWithZeroValue(string $type, string $selectedAndExpectedOption): void
307+
{
308+
$crawler = self::createPantherClient()->request('GET', self::$baseUri.'/form.html');
309+
$element = $crawler->filterXPath("//form[@id='zero-form-$type']/input")->getElement(0);
310+
311+
$c = new WebDriverCheckbox($element);
312+
$c->selectByValue($selectedAndExpectedOption);
313+
314+
$selectedValues = [];
315+
foreach ($c->getAllSelectedOptions() as $option) {
316+
$selectedValues[] = $option->getAttribute('value');
317+
}
318+
$this->assertSame([$selectedAndExpectedOption], $selectedValues);
319+
}
320+
321+
public static function selectByValueDataProviderWithZeroValue(): iterable
322+
{
323+
yield ['checkbox', '0'];
324+
yield ['radio', '0'];
325+
}
302326
}

tests/fixtures/form.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,16 @@
6464
<input type="radio" form="another-form" name="j3" value="j3c" id="j3c">
6565

6666
<input type="submit" form="another-form" id="special-submit" formmethod="DELETE" >
67+
68+
<form id="zero-form-checkbox" action="form-handle.php" method="GET">
69+
<input type="checkbox" name="z1[]" value="z1a">
70+
<input type="checkbox" name="z1[]" value="0">
71+
</form>
72+
73+
<form id="zero-form-radio" action="form-handle.php" method="GET">
74+
<input type="radio" name="z2" value="z2a">
75+
<input type="radio" name="z2" value="0">
76+
</form>
77+
6778
</body>
6879
</html>

0 commit comments

Comments
 (0)