Skip to content

Commit

Permalink
fix: checkbox/radio with value '0' not accessible with byValue() (#627)
Browse files Browse the repository at this point in the history
Co-authored-by: Kévin Dunglas <[email protected]>
  • Loading branch information
WubbleWobble and dunglas authored Jan 7, 2025
1 parent b3cb34e commit 0fa5f65
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/WebDriver/WebDriverCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private function byVisibleText($text, $partial = false, $select = true): void

private function getRelatedElements($value = null): array
{
$valueSelector = $value ? \sprintf(' and @value = %s', XPathEscaper::escapeQuotes($value)) : '';
$valueSelector = null === $value ? '' : \sprintf(' and @value = %s', XPathEscaper::escapeQuotes($value));
if (null === $formId = $this->element->getAttribute('form')) {
$form = $this->element->findElement(WebDriverBy::xpath('ancestor::form'));
if ('' === $formId = (string) $form->getAttribute('id')) {
Expand Down
24 changes: 24 additions & 0 deletions tests/WebDriver/WebDriverCheckBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,28 @@ public function testWebDriverCheckboxDeselectByVisiblePartialTextRadio(): void
$c = new WebDriverCheckbox($element);
$c->deselectByVisiblePartialText('AB');
}

/**
* @dataProvider selectByValueDataProviderWithZeroValue
*/
public function testWebDriverCheckboxSelectByValueWithZeroValue(string $type, string $selectedAndExpectedOption): void
{
$crawler = self::createPantherClient()->request('GET', self::$baseUri.'/form.html');
$element = $crawler->filterXPath("//form[@id='zero-form-$type']/input")->getElement(0);

$c = new WebDriverCheckbox($element);
$c->selectByValue($selectedAndExpectedOption);

$selectedValues = [];
foreach ($c->getAllSelectedOptions() as $option) {
$selectedValues[] = $option->getAttribute('value');
}
$this->assertSame([$selectedAndExpectedOption], $selectedValues);
}

public static function selectByValueDataProviderWithZeroValue(): iterable
{
yield ['checkbox', '0'];
yield ['radio', '0'];
}
}
11 changes: 11 additions & 0 deletions tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,16 @@
<input type="radio" form="another-form" name="j3" value="j3c" id="j3c">

<input type="submit" form="another-form" id="special-submit" formmethod="DELETE" >

<form id="zero-form-checkbox" action="form-handle.php" method="GET">
<input type="checkbox" name="z1[]" value="z1a">
<input type="checkbox" name="z1[]" value="0">
</form>

<form id="zero-form-radio" action="form-handle.php" method="GET">
<input type="radio" name="z2" value="z2a">
<input type="radio" name="z2" value="0">
</form>

</body>
</html>

0 comments on commit 0fa5f65

Please sign in to comment.