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

Commit 0e90e92

Browse files
committed
Merge branch 'pull/621' into community
2 parents d1ad0b1 + e8accbd commit 0e90e92

File tree

4 files changed

+194
-91
lines changed

4 files changed

+194
-91
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: 100 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,73 +32,107 @@ protected function setUp()
3232

3333
public function testIsMultiple()
3434
{
35-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
36-
$this->assertTrue($c->isMultiple());
35+
$checkboxes = new WebDriverCheckboxes(
36+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
37+
);
38+
39+
$this->assertTrue($checkboxes->isMultiple());
3740
}
3841

3942
public function testGetOptions()
4043
{
41-
$c = new WebDriverCheckboxes(
44+
$checkboxes = new WebDriverCheckboxes(
4245
$this->driver->findElement(WebDriverBy::xpath('//form[2]//input[@type="checkbox"]'))
4346
);
44-
$this->assertNotEmpty($c->getOptions());
47+
48+
$this->assertNotEmpty($checkboxes->getOptions());
4549
}
4650

4751
public function testGetFirstSelectedOption()
4852
{
49-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
50-
$c->selectByValue('j2a');
51-
$this->assertSame('j2a', $c->getFirstSelectedOption()->getAttribute('value'));
53+
$checkboxes = new WebDriverCheckboxes(
54+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
55+
);
56+
57+
$checkboxes->selectByValue('j2a');
58+
59+
$this->assertSame('j2a', $checkboxes->getFirstSelectedOption()->getAttribute('value'));
60+
}
61+
62+
public function testShouldGetFirstSelectedOptionConsideringOnlyElementsAssociatedWithCurrentForm()
63+
{
64+
$checkboxes = new WebDriverCheckboxes(
65+
$this->driver->findElement(WebDriverBy::xpath('//input[@id="j5b"]'))
66+
);
67+
68+
$this->assertEquals('j5b', $checkboxes->getFirstSelectedOption()->getAttribute('value'));
69+
}
70+
71+
public function testShouldGetFirstSelectedOptionConsideringOnlyElementsAssociatedWithCurrentFormWithoutId()
72+
{
73+
$checkboxes = new WebDriverCheckboxes(
74+
$this->driver->findElement(WebDriverBy::xpath('//input[@id="j5d"]'))
75+
);
76+
77+
$this->assertEquals('j5c', $checkboxes->getFirstSelectedOption()->getAttribute('value'));
5278
}
5379

5480
public function testSelectByValue()
5581
{
5682
$selectedOptions = ['j2b', 'j2c'];
5783

58-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
84+
$checkboxes = new WebDriverCheckboxes(
85+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
86+
);
5987
foreach ($selectedOptions as $index => $selectedOption) {
60-
$c->selectByValue($selectedOption);
88+
$checkboxes->selectByValue($selectedOption);
6189
}
6290

6391
$selectedValues = [];
64-
foreach ($c->getAllSelectedOptions() as $option) {
92+
foreach ($checkboxes->getAllSelectedOptions() as $option) {
6593
$selectedValues[] = $option->getAttribute('value');
6694
}
6795
$this->assertSame($selectedOptions, $selectedValues);
6896
}
6997

7098
public function testSelectByValueInvalid()
7199
{
72-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
100+
$checkboxes = new WebDriverCheckboxes(
101+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
102+
);
73103

74104
$this->expectException(NoSuchElementException::class);
75105
$this->expectExceptionMessage('Cannot locate checkbox with value: notexist');
76-
$c->selectByValue('notexist');
106+
$checkboxes->selectByValue('notexist');
77107
}
78108

79109
public function testSelectByIndex()
80110
{
81111
$selectedOptions = [1 => 'j2b', 2 => 'j2c'];
82112

83-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
113+
$checkboxes = new WebDriverCheckboxes(
114+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
115+
);
84116
foreach ($selectedOptions as $index => $selectedOption) {
85-
$c->selectByIndex($index);
117+
$checkboxes->selectByIndex($index);
86118
}
87119

88120
$selectedValues = [];
89-
foreach ($c->getAllSelectedOptions() as $option) {
121+
foreach ($checkboxes->getAllSelectedOptions() as $option) {
90122
$selectedValues[] = $option->getAttribute('value');
91123
}
92124
$this->assertSame(array_values($selectedOptions), $selectedValues);
93125
}
94126

95127
public function testSelectByIndexInvalid()
96128
{
97-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
129+
$checkboxes = new WebDriverCheckboxes(
130+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
131+
);
98132

99133
$this->expectException(NoSuchElementException::class);
100134
$this->expectExceptionMessage('Cannot locate checkbox with index: ' . PHP_INT_MAX);
101-
$c->selectByIndex(PHP_INT_MAX);
135+
$checkboxes->selectByIndex(PHP_INT_MAX);
102136
}
103137

104138
/**
@@ -109,9 +143,13 @@ public function testSelectByIndexInvalid()
109143
*/
110144
public function testSelectByVisibleText($text, $value)
111145
{
112-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
113-
$c->selectByVisibleText($text);
114-
$this->assertSame($value, $c->getFirstSelectedOption()->getAttribute('value'));
146+
$checkboxes = new WebDriverCheckboxes(
147+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
148+
);
149+
150+
$checkboxes->selectByVisibleText($text);
151+
152+
$this->assertSame($value, $checkboxes->getFirstSelectedOption()->getAttribute('value'));
115153
}
116154

117155
/**
@@ -133,9 +171,13 @@ public function selectByVisibleTextDataProvider()
133171
*/
134172
public function testSelectByVisiblePartialText($text, $value)
135173
{
136-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
137-
$c->selectByVisiblePartialText($text);
138-
$this->assertSame($value, $c->getFirstSelectedOption()->getAttribute('value'));
174+
$checkboxes = new WebDriverCheckboxes(
175+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
176+
);
177+
178+
$checkboxes->selectByVisiblePartialText($text);
179+
180+
$this->assertSame($value, $checkboxes->getFirstSelectedOption()->getAttribute('value'));
139181
}
140182

141183
/**
@@ -151,51 +193,61 @@ public function selectByVisiblePartialTextDataProvider()
151193

152194
public function testDeselectAll()
153195
{
154-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
196+
$checkboxes = new WebDriverCheckboxes(
197+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
198+
);
155199

156-
$c->selectByIndex(0);
157-
$this->assertCount(1, $c->getAllSelectedOptions());
158-
$c->deselectAll();
159-
$this->assertEmpty($c->getAllSelectedOptions());
200+
$checkboxes->selectByIndex(0);
201+
$this->assertCount(1, $checkboxes->getAllSelectedOptions());
202+
$checkboxes->deselectAll();
203+
$this->assertEmpty($checkboxes->getAllSelectedOptions());
160204
}
161205

162206
public function testDeselectByIndex()
163207
{
164-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
208+
$checkboxes = new WebDriverCheckboxes(
209+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
210+
);
165211

166-
$c->selectByIndex(0);
167-
$this->assertCount(1, $c->getAllSelectedOptions());
168-
$c->deselectByIndex(0);
169-
$this->assertEmpty($c->getAllSelectedOptions());
212+
$checkboxes->selectByIndex(0);
213+
$this->assertCount(1, $checkboxes->getAllSelectedOptions());
214+
$checkboxes->deselectByIndex(0);
215+
$this->assertEmpty($checkboxes->getAllSelectedOptions());
170216
}
171217

172218
public function testDeselectByValue()
173219
{
174-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
220+
$checkboxes = new WebDriverCheckboxes(
221+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
222+
);
175223

176-
$c->selectByValue('j2a');
177-
$this->assertCount(1, $c->getAllSelectedOptions());
178-
$c->deselectByValue('j2a');
179-
$this->assertEmpty($c->getAllSelectedOptions());
224+
$checkboxes->selectByValue('j2a');
225+
$this->assertCount(1, $checkboxes->getAllSelectedOptions());
226+
$checkboxes->deselectByValue('j2a');
227+
$this->assertEmpty($checkboxes->getAllSelectedOptions());
180228
}
181229

182230
public function testDeselectByVisibleText()
183231
{
184-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
232+
$checkboxes = new WebDriverCheckboxes(
233+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
234+
);
185235

186-
$c->selectByVisibleText('J 2 B');
187-
$this->assertCount(1, $c->getAllSelectedOptions());
188-
$c->deselectByVisibleText('J 2 B');
189-
$this->assertEmpty($c->getAllSelectedOptions());
236+
$checkboxes->selectByVisibleText('J 2 B');
237+
$this->assertCount(1, $checkboxes->getAllSelectedOptions());
238+
$checkboxes->deselectByVisibleText('J 2 B');
239+
$this->assertEmpty($checkboxes->getAllSelectedOptions());
190240
}
191241

192242
public function testDeselectByVisiblePartialText()
193243
{
194-
$c = new WebDriverCheckboxes($this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]')));
244+
$checkboxes = new WebDriverCheckboxes(
245+
$this->driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'))
246+
);
195247

196-
$c->selectByVisiblePartialText('2C');
197-
$this->assertCount(1, $c->getAllSelectedOptions());
198-
$c->deselectByVisiblePartialText('2C');
199-
$this->assertEmpty($c->getAllSelectedOptions());
248+
$checkboxes->selectByVisiblePartialText('2C');
249+
$this->assertCount(1, $checkboxes->getAllSelectedOptions());
250+
$checkboxes->deselectByVisiblePartialText('2C');
251+
$this->assertEmpty($checkboxes->getAllSelectedOptions());
200252
}
201253
}

0 commit comments

Comments
 (0)