Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 12ac107

Browse files
oleg-andreyevOndraM
authored andcommitted
Improved xpath matching related elements to handle input associated to different <form>
Added @Form attribute check, because it's possible to put <input/> into a form which is not related to it, see MDN https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form
1 parent d1ad0b1 commit 12ac107

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

lib/AbstractWebDriverCheckboxOrRadio.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ protected function getRelatedElements($value = null)
218218
}
219219
}
220220

221-
return $this->element->findElements(WebDriverBy::xpath(sprintf(
222-
'//form[@id = %1$s]//input[@name = %2$s%3$s] | //input[@form = %1$s and @name = %2$s%3$s]',
223-
XPathEscaper::escapeQuotes($formId),
224-
XPathEscaper::escapeQuotes($this->name),
225-
$valueSelector
226-
)));
221+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form
222+
return $this->element->findElements(
223+
WebDriverBy::xpath(sprintf(
224+
'//form[@id = %1$s]//input[@name = %2$s%3$s'
225+
. ' and ((boolean(@form) = true() and @form = %1$s) or boolean(@form) = false())]'
226+
. ' | //input[@form = %1$s and @name = %2$s%3$s]',
227+
XPathEscaper::escapeQuotes($formId),
228+
XPathEscaper::escapeQuotes($this->name),
229+
$valueSelector
230+
))
231+
);
227232
}
228233

229234
/**

tests/functional/WebDriverCheckboxesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function testGetFirstSelectedOption()
5151
$this->assertSame('j2a', $c->getFirstSelectedOption()->getAttribute('value'));
5252
}
5353

54+
public function testGetFirstSelectedOptionWithSameNameDifferentForm()
55+
{
56+
$radio = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@id="j5b"]')));
57+
$this->assertEquals('j5b', $radio->getFirstSelectedOption()->getAttribute('value'));
58+
}
59+
5460
public function testSelectByValue()
5561
{
5662
$selectedOptions = ['j2b', 'j2c'];

tests/functional/WebDriverRadiosTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function testGetFirstSelectedOption()
5555
$this->assertSame('j3a', $c->getFirstSelectedOption()->getAttribute('value'));
5656
}
5757

58+
public function testGetFirstSelectedOptionWithSameNameDifferentForm()
59+
{
60+
$radio = new WebDriverRadios($this->driver->findElement(WebDriverBy::xpath('//input[@id="j4b"]')));
61+
$this->assertEquals('j4b', $radio->getFirstSelectedOption()->getAttribute('value'));
62+
}
63+
5864
public function testSelectByValue()
5965
{
6066
$c = new WebDriverRadios($this->driver->findElement(WebDriverBy::xpath('//input[@type="radio"]')));

tests/functional/web/form_checkbox_radio.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<input type="radio" name="j3" value="j3b">
2828
</label>
2929

30+
<label for="j4a">J4A</label>
31+
<input type="radio" form="yet-another-form" name="j4" value="j4a" id="j4a" checked/>
32+
33+
<label for="j5a">J5A</label>
34+
<input type="checkbox" form="yet-another-form" name="j5" value="j5a" id="j5a" checked/>
35+
3036
<input type="submit" value="OK">
3137
</form>
3238

@@ -36,7 +42,13 @@
3642
<label for="j3c">J3C</label>
3743
<input type="radio" form="another-form" name="j3" value="j3c" id="j3c">
3844

39-
<form>
45+
<label for="j4b">J4B</label>
46+
<input type="radio" form="another-form" name="j4" value="j4b" id="j4b" checked>
47+
48+
<label for="j5b">J5B</label>
49+
<input type="checkbox" form="another-form" name="j5" value="j5b" id="j5b" checked>
50+
51+
<form id="yet-another-form">
4052
<input type="checkbox" name="single-cb">
4153
</form>
4254
</body>

0 commit comments

Comments
 (0)