Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/select_class_tests.py
4011 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.common.exceptions import NoSuchElementException, UnexpectedTagNameException
21
from selenium.webdriver.common.by import By
22
from selenium.webdriver.support.ui import Select
23
24
disabledSelect = {"name": "no-select", "values": ["Foo"]}
25
disabledSingleSelect = {"name": "single_disabled", "values": ["Enabled", "Disabled"]}
26
disabledMultiSelect = {"name": "multi_disabled", "values": ["Enabled", "Disabled"]}
27
invisibleMultiSelect = {"id": "invisible_multi_select", "values": ["Apples", "Pears", "Oranges", "Lemons"]}
28
singleSelectValues1 = {
29
"name": "selectomatic",
30
"values": ["One", "Two", "Four", "Still learning how to count, apparently"],
31
}
32
singleSelectValues2 = {"name": "redirect", "values": ["One", "Two"]}
33
singleSelectValuesWithSpaces = {
34
"name": "select_with_spaces",
35
"values": ["One", "Two", "Four", "Still learning how to count, apparently"],
36
}
37
multiSelectValues1 = {"name": "multi", "values": ["Eggs", "Ham", "Sausages", "Onion gravy"]}
38
multiSelectValues2 = {"name": "select_empty_multiple", "values": ["select_1", "select_2", "select_3", "select_4"]}
39
40
41
def test_select_by_index_single(driver, pages):
42
pages.load("formPage.html")
43
44
for select in [singleSelectValues1]:
45
sel = Select(driver.find_element(By.NAME, select["name"]))
46
for x in range(len(select["values"])):
47
sel.select_by_index(x)
48
assert sel.first_selected_option.text == select["values"][x]
49
50
51
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
52
def test_raises_exception_select_by_index_single_disabled(driver, pages):
53
pages.load("formPage.html")
54
sel = Select(driver.find_element(By.NAME, disabledSingleSelect["name"]))
55
with pytest.raises(NotImplementedError):
56
sel.select_by_index(1)
57
58
59
def test_select_by_value_single(driver, pages):
60
pages.load("formPage.html")
61
for select in [singleSelectValues1]:
62
sel = Select(driver.find_element(By.NAME, select["name"]))
63
for x in range(len(select["values"])):
64
sel.select_by_value(select["values"][x].lower())
65
assert sel.first_selected_option.text == select["values"][x]
66
67
68
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
69
def test_raises_exception_select_by_value_single_disabled(driver, pages):
70
pages.load("formPage.html")
71
sel = Select(driver.find_element(By.NAME, disabledSingleSelect["name"]))
72
with pytest.raises(NotImplementedError):
73
sel.select_by_value(disabledSingleSelect["values"][1].lower())
74
75
76
def test_select_by_visible_text_single(driver, pages):
77
pages.load("formPage.html")
78
79
for select in [singleSelectValues1]:
80
sel = Select(driver.find_element(By.NAME, select["name"]))
81
for x in range(len(select["values"])):
82
print(select["values"][x])
83
sel.select_by_visible_text(select["values"][x])
84
assert sel.first_selected_option.text == select["values"][x]
85
86
87
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
88
def test_raises_exception_select_by_text_single_disabled(driver, pages):
89
pages.load("formPage.html")
90
sel = Select(driver.find_element(By.NAME, disabledSingleSelect["name"]))
91
with pytest.raises(NotImplementedError):
92
sel.select_by_visible_text(disabledSingleSelect["values"][1])
93
94
95
def test_select_by_index_multiple(driver, pages):
96
pages.load("formPage.html")
97
98
for select in [multiSelectValues1, multiSelectValues2]:
99
sel = Select(driver.find_element(By.NAME, select["name"]))
100
sel.deselect_all()
101
for x in range(len(select["values"])):
102
sel.select_by_index(x)
103
selected = sel.all_selected_options
104
assert len(selected) == x + 1
105
for j in range(len(selected)):
106
assert selected[j].text == select["values"][j]
107
108
109
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
110
def test_raises_exception_select_by_index_multiple_disabled(driver, pages):
111
pages.load("formPage.html")
112
113
sel = Select(driver.find_element(By.NAME, disabledMultiSelect["name"]))
114
with pytest.raises(NotImplementedError):
115
sel.select_by_index(1)
116
117
118
def test_select_by_value_multiple(driver, pages):
119
pages.load("formPage.html")
120
121
for select in [multiSelectValues1, multiSelectValues2]:
122
sel = Select(driver.find_element(By.NAME, select["name"]))
123
sel.deselect_all()
124
for x in range(len(select["values"])):
125
sel.select_by_value(select["values"][x].lower())
126
selected = sel.all_selected_options
127
assert len(selected) == x + 1
128
for j in range(len(selected)):
129
assert selected[j].text == select["values"][j]
130
131
132
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
133
def test_raises_exception_select_by_value_multiple_disabled(driver, pages):
134
pages.load("formPage.html")
135
136
sel = Select(driver.find_element(By.NAME, disabledMultiSelect["name"]))
137
with pytest.raises(NotImplementedError):
138
sel.select_by_value(disabledMultiSelect["values"][1].lower())
139
140
141
def test_select_by_visible_text_multiple(driver, pages):
142
pages.load("formPage.html")
143
144
for select in [multiSelectValues1, multiSelectValues2]:
145
sel = Select(driver.find_element(By.NAME, select["name"]))
146
sel.deselect_all()
147
for x in range(len(select["values"])):
148
sel.select_by_visible_text(select["values"][x])
149
selected = sel.all_selected_options
150
assert len(selected) == x + 1
151
for j in range(len(selected)):
152
assert selected[j].text == select["values"][j]
153
154
155
@pytest.mark.xfail_safari(reason="options incorrectly reported as enabled")
156
def test_raises_exception_select_by_text_multiple_disabled(driver, pages):
157
pages.load("formPage.html")
158
159
sel = Select(driver.find_element(By.NAME, disabledMultiSelect["name"]))
160
with pytest.raises(NotImplementedError):
161
sel.select_by_visible_text(disabledMultiSelect["values"][1])
162
163
164
def test_raises_exception_select_by_text_multiple_hidden(driver, pages):
165
pages.load("formPage.html")
166
167
sel = Select(driver.find_element(By.ID, invisibleMultiSelect["id"]))
168
for option in invisibleMultiSelect["values"]:
169
with pytest.raises(NoSuchElementException):
170
sel.select_by_visible_text(option)
171
172
173
def test_deselect_all_single(driver, pages):
174
pages.load("formPage.html")
175
for select in [singleSelectValues1, singleSelectValues2]:
176
with pytest.raises(NotImplementedError):
177
Select(driver.find_element(By.NAME, select["name"])).deselect_all()
178
179
180
def test_deselect_all_multiple(driver, pages):
181
pages.load("formPage.html")
182
for select in [multiSelectValues1, multiSelectValues2]:
183
sel = Select(driver.find_element(By.NAME, select["name"]))
184
sel.deselect_all()
185
assert len(sel.all_selected_options) == 0
186
187
188
def test_deselect_by_index_single(driver, pages):
189
pages.load("formPage.html")
190
for select in [singleSelectValues1, singleSelectValues2]:
191
with pytest.raises(NotImplementedError):
192
Select(driver.find_element(By.NAME, select["name"])).deselect_by_index(0)
193
194
195
def test_deselect_by_value_single(driver, pages):
196
pages.load("formPage.html")
197
for select in [singleSelectValues1, singleSelectValues2]:
198
with pytest.raises(NotImplementedError):
199
Select(driver.find_element(By.NAME, select["name"])).deselect_by_value(select["values"][0].lower())
200
201
202
def test_deselect_by_visible_text_single(driver, pages):
203
pages.load("formPage.html")
204
for select in [singleSelectValues1, singleSelectValues2]:
205
with pytest.raises(NotImplementedError):
206
Select(driver.find_element(By.NAME, select["name"])).deselect_by_visible_text(select["values"][0])
207
208
209
def test_deselect_by_index_multiple(driver, pages):
210
pages.load("formPage.html")
211
for select in [multiSelectValues1, multiSelectValues2]:
212
sel = Select(driver.find_element(By.NAME, select["name"]))
213
sel.deselect_all()
214
sel.select_by_index(0)
215
sel.select_by_index(1)
216
sel.select_by_index(2)
217
sel.select_by_index(3)
218
sel.deselect_by_index(1)
219
sel.deselect_by_index(3)
220
selected = sel.all_selected_options
221
assert len(selected) == 2
222
assert selected[0].text == select["values"][0]
223
assert selected[1].text == select["values"][2]
224
225
226
def test_deselect_by_value_multiple(driver, pages):
227
pages.load("formPage.html")
228
for select in [multiSelectValues1, multiSelectValues2]:
229
sel = Select(driver.find_element(By.NAME, select["name"]))
230
sel.deselect_all()
231
sel.select_by_index(0)
232
sel.select_by_index(1)
233
sel.select_by_index(2)
234
sel.select_by_index(3)
235
sel.deselect_by_value(select["values"][1].lower())
236
sel.deselect_by_value(select["values"][3].lower())
237
selected = sel.all_selected_options
238
assert len(selected) == 2
239
assert selected[0].text == select["values"][0]
240
assert selected[1].text == select["values"][2]
241
242
243
def test_deselect_by_visible_text_multiple(driver, pages):
244
pages.load("formPage.html")
245
for select in [multiSelectValues1, multiSelectValues2]:
246
sel = Select(driver.find_element(By.NAME, select["name"]))
247
sel.deselect_all()
248
sel.select_by_index(0)
249
sel.select_by_index(1)
250
sel.select_by_index(2)
251
sel.select_by_index(3)
252
sel.deselect_by_visible_text(select["values"][1])
253
sel.deselect_by_visible_text(select["values"][3])
254
selected = sel.all_selected_options
255
assert len(selected) == 2
256
assert selected[0].text == select["values"][0]
257
assert selected[1].text == select["values"][2]
258
259
260
def test_get_options(driver, pages):
261
pages.load("formPage.html")
262
for select in [singleSelectValues1, singleSelectValues2, multiSelectValues1, multiSelectValues2]:
263
opts = Select(driver.find_element(By.NAME, select["name"])).options
264
assert len(opts) == len(select["values"])
265
for i in range(len(opts)):
266
assert opts[i].text == select["values"][i]
267
268
269
def test_get_all_selected_options_single(driver, pages):
270
pages.load("formPage.html")
271
for select in [singleSelectValues1, singleSelectValues2, disabledSelect]:
272
opts = Select(driver.find_element(By.NAME, select["name"])).all_selected_options
273
assert len(opts) == 1
274
assert opts[0].text == select["values"][0]
275
276
277
def test_get_all_selected_options_multiple(driver, pages):
278
pages.load("formPage.html")
279
opts = Select(driver.find_element(By.NAME, multiSelectValues1["name"])).all_selected_options
280
assert len(opts) == 2
281
assert opts[0].text, multiSelectValues1["values"][0]
282
assert opts[1].text, multiSelectValues1["values"][2]
283
opts = Select(driver.find_element(By.NAME, multiSelectValues2["name"])).all_selected_options
284
assert len(opts) == 0
285
286
287
def test_get_first_selected_option_single(driver, pages):
288
pages.load("formPage.html")
289
for select in [singleSelectValues1, singleSelectValues2]:
290
opt = Select(driver.find_element(By.NAME, select["name"])).first_selected_option
291
assert opt.text == select["values"][0]
292
293
294
def test_get_first_selected_option_multiple(driver, pages):
295
pages.load("formPage.html")
296
opt = Select(driver.find_element(By.NAME, multiSelectValues1["name"])).first_selected_option
297
assert opt.text == multiSelectValues1["values"][0]
298
opt = Select(driver.find_element(By.NAME, multiSelectValues2["name"])).all_selected_options
299
assert len(opt) == 0
300
301
302
def test_raises_exception_for_invalid_tag_name(driver, pages):
303
pages.load("formPage.html")
304
with pytest.raises(UnexpectedTagNameException):
305
Select(driver.find_element(By.TAG_NAME, "div"))
306
307
308
def test_deselect_by_index_non_existent(driver, pages):
309
pages.load("formPage.html")
310
for select in [multiSelectValues1, multiSelectValues2]:
311
with pytest.raises(NoSuchElementException):
312
Select(driver.find_element(By.NAME, select["name"])).deselect_by_index(10)
313
314
315
def test_deselect_by_value_non_existent(driver, pages):
316
pages.load("formPage.html")
317
for select in [multiSelectValues1, multiSelectValues2]:
318
with pytest.raises(NoSuchElementException):
319
Select(driver.find_element(By.NAME, select["name"])).deselect_by_value("not there")
320
321
322
def test_deselect_by_text_non_existent(driver, pages):
323
pages.load("formPage.html")
324
for select in [multiSelectValues1, multiSelectValues2]:
325
with pytest.raises(NoSuchElementException):
326
Select(driver.find_element(By.NAME, select["name"])).deselect_by_visible_text("not there")
327
328