/
WebDriverSelect.php
245 lines (207 loc) · 7.01 KB
/
WebDriverSelect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\UnexpectedTagNameException;
use Facebook\WebDriver\Exception\UnsupportedOperationException;
use Facebook\WebDriver\Support\XPathEscaper;
/**
* Models a default HTML `<select>` tag, providing helper methods to select and deselect options.
*/
class WebDriverSelect implements WebDriverSelectInterface
{
/** @var WebDriverElement */
private $element;
/** @var bool */
private $isMulti;
public function __construct(WebDriverElement $element)
{
$tag_name = $element->getTagName();
if ($tag_name !== 'select') {
throw new UnexpectedTagNameException('select', $tag_name);
}
$this->element = $element;
$value = $element->getAttribute('multiple');
$this->isMulti = $value === 'true';
}
public function isMultiple()
{
return $this->isMulti;
}
public function getOptions()
{
return $this->element->findElements(WebDriverBy::tagName('option'));
}
public function getAllSelectedOptions()
{
$selected_options = [];
foreach ($this->getOptions() as $option) {
if ($option->isSelected()) {
$selected_options[] = $option;
if (!$this->isMultiple()) {
return $selected_options;
}
}
}
return $selected_options;
}
public function getFirstSelectedOption()
{
foreach ($this->getOptions() as $option) {
if ($option->isSelected()) {
return $option;
}
}
throw new NoSuchElementException('No options are selected');
}
public function selectByIndex($index)
{
foreach ($this->getOptions() as $option) {
if ($option->getAttribute('index') === (string) $index) {
$this->selectOption($option);
return;
}
}
throw new NoSuchElementException(sprintf('Cannot locate option with index: %d', $index));
}
public function selectByValue($value)
{
$matched = false;
$xpath = './/option[@value = ' . XPathEscaper::escapeQuotes($value) . ']';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->selectOption($option);
if (!$this->isMultiple()) {
return;
}
$matched = true;
}
if (!$matched) {
throw new NoSuchElementException(
sprintf('Cannot locate option with value: %s', $value)
);
}
}
public function selectByVisibleText($text)
{
$matched = false;
$xpath = './/option[normalize-space(.) = ' . XPathEscaper::escapeQuotes($text) . ']';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->selectOption($option);
if (!$this->isMultiple()) {
return;
}
$matched = true;
}
// Since the mechanism of getting the text in xpath is not the same as
// webdriver, use the expensive getText() to check if nothing is matched.
if (!$matched) {
foreach ($this->getOptions() as $option) {
if ($option->getText() === $text) {
$this->selectOption($option);
if (!$this->isMultiple()) {
return;
}
$matched = true;
}
}
}
if (!$matched) {
throw new NoSuchElementException(
sprintf('Cannot locate option with text: %s', $text)
);
}
}
public function selectByVisiblePartialText($text)
{
$matched = false;
$xpath = './/option[contains(normalize-space(.), ' . XPathEscaper::escapeQuotes($text) . ')]';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->selectOption($option);
if (!$this->isMultiple()) {
return;
}
$matched = true;
}
if (!$matched) {
throw new NoSuchElementException(
sprintf('Cannot locate option with text: %s', $text)
);
}
}
public function deselectAll()
{
if (!$this->isMultiple()) {
throw new UnsupportedOperationException('You may only deselect all options of a multi-select');
}
foreach ($this->getOptions() as $option) {
$this->deselectOption($option);
}
}
public function deselectByIndex($index)
{
if (!$this->isMultiple()) {
throw new UnsupportedOperationException('You may only deselect options of a multi-select');
}
foreach ($this->getOptions() as $option) {
if ($option->getAttribute('index') === (string) $index) {
$this->deselectOption($option);
return;
}
}
}
public function deselectByValue($value)
{
if (!$this->isMultiple()) {
throw new UnsupportedOperationException('You may only deselect options of a multi-select');
}
$xpath = './/option[@value = ' . XPathEscaper::escapeQuotes($value) . ']';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->deselectOption($option);
}
}
public function deselectByVisibleText($text)
{
if (!$this->isMultiple()) {
throw new UnsupportedOperationException('You may only deselect options of a multi-select');
}
$xpath = './/option[normalize-space(.) = ' . XPathEscaper::escapeQuotes($text) . ']';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->deselectOption($option);
}
}
public function deselectByVisiblePartialText($text)
{
if (!$this->isMultiple()) {
throw new UnsupportedOperationException('You may only deselect options of a multi-select');
}
$xpath = './/option[contains(normalize-space(.), ' . XPathEscaper::escapeQuotes($text) . ')]';
$options = $this->element->findElements(WebDriverBy::xpath($xpath));
foreach ($options as $option) {
$this->deselectOption($option);
}
}
/**
* Mark option selected
* @param WebDriverElement $option
*/
protected function selectOption(WebDriverElement $option)
{
if (!$option->isSelected()) {
$option->click();
}
}
/**
* Mark option not selected
* @param WebDriverElement $option
*/
protected function deselectOption(WebDriverElement $option)
{
if ($option->isSelected()) {
$option->click();
}
}
}