Skip to content

Commit 1c6fa4d

Browse files
committed
Merge pull request robotframework#1 from schminitz/use-selenium-select-class
See robotframework#68
2 parents 2c4a9a4 + 79226d9 commit 1c6fa4d

File tree

1 file changed

+122
-26
lines changed

1 file changed

+122
-26
lines changed

src/Selenium2Library/keywords/_selectelement.py

Lines changed: 122 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ def get_list_items(self, locator):
1818
def get_selected_list_label(self, locator):
1919
"""Returns the visible label of the selected element from the select list identified by `locator`.
2020
21-
Fails if there are zero or more than one selection.
22-
2321
Select list keywords work on both lists and combo boxes. Key attributes for
2422
select lists are `id` and `name`. See `introduction` for details about
2523
locating elements.
2624
"""
27-
labels = self.get_selected_list_labels(locator)
28-
if len(labels) != 1:
29-
raise ValueError("Select list with locator '%s' does not have a single selected value")
30-
return labels[0]
25+
select = self._get_select_list(locator)
26+
return select.first_selected_option.get_attribute('label')
3127

3228
def get_selected_list_labels(self, locator):
3329
"""Returns the visible labels of selected elements (as a list) from the select list identified by `locator`.
@@ -47,16 +43,13 @@ def get_selected_list_value(self, locator):
4743
"""Returns the value of the selected element from the select list identified by `locator`.
4844
4945
Return value is read from `value` attribute of the selected element.
50-
Fails if there are zero or more than one selection.
5146
5247
Select list keywords work on both lists and combo boxes. Key attributes for
5348
select lists are `id` and `name`. See `introduction` for details about
5449
locating elements.
5550
"""
56-
values = self.get_selected_list_values(locator)
57-
if len(values) != 1:
58-
raise ValueError("Select list with locator '%s' does not have a single selected value")
59-
return values[0]
51+
select = self._get_select_list(locator)
52+
return select.first_selected_option.get_attribute('value')
6053

6154
def get_selected_list_values(self, locator):
6255
"""Returns the values of selected elements (as a list) from the select list identified by `locator`.
@@ -158,18 +151,18 @@ def select_from_list(self, locator, *items):
158151
value will be selected. If the target list is a multi-selection list,
159152
and `*items` is an empty list, all values of the list will be selected.
160153
154+
*items try to select by value then by label.
155+
156+
It's faster to use 'by index/value/label' functions.
157+
161158
Select list keywords work on both lists and combo boxes. Key attributes for
162159
select lists are `id` and `name`. See `introduction` for details about
163160
locating elements.
164161
"""
165162
items_str = items and "option(s) '%s'" % ", ".join(items) or "all options"
166163
self._info("Selecting %s from list '%s'." % (items_str, locator))
167-
items = list(items)
168164

169165
select = self._get_select_list(locator)
170-
#is_multi_select = select.is_multiple
171-
#if len(options)>1 and not is_multi_select:
172-
# self._warn("Trying to select multiple options while list '%s' is single select list." % (locator))
173166

174167
if not items:
175168
for i in range(len(select.options)):
@@ -182,19 +175,70 @@ def select_from_list(self, locator, *items):
182175
try: select.select_by_visible_text(item)
183176
except: continue
184177

178+
def select_from_list_by_index(self, locator, *indexes):
179+
"""Selects `*indexes` from list identified by `locator`
180+
181+
Select list keywords work on both lists and combo boxes. Key attributes for
182+
select lists are `id` and `name`. See `introduction` for details about
183+
locating elements.
184+
"""
185+
if not indexes:
186+
raise ValueError("No index given.")
187+
items_str = "index(es) '%s'" % ", ".join(indexes)
188+
self._info("Selecting %s from list '%s'." % (items_str, locator))
189+
190+
select = self._get_select_list(locator)
191+
for index in indexes:
192+
select.select_by_index(int(index))
193+
194+
def select_from_list_by_value(self, locator, *values):
195+
"""Selects `*values` from list identified by `locator`
196+
197+
Select list keywords work on both lists and combo boxes. Key attributes for
198+
select lists are `id` and `name`. See `introduction` for details about
199+
locating elements.
200+
"""
201+
if not values:
202+
raise ValueError("No value given.")
203+
items_str = "value(s) '%s'" % ", ".join(values)
204+
self._info("Selecting %s from list '%s'." % (items_str, locator))
205+
206+
select = self._get_select_list(locator)
207+
for value in values:
208+
select.select_by_value(value)
209+
210+
def select_from_list_by_label(self, locator, *labels):
211+
"""Selects `*labels` from list identified by `locator`
212+
213+
Select list keywords work on both lists and combo boxes. Key attributes for
214+
select lists are `id` and `name`. See `introduction` for details about
215+
locating elements.
216+
"""
217+
if not labels:
218+
raise ValueError("No value given.")
219+
items_str = "label(s) '%s'" % ", ".join(labels)
220+
self._info("Selecting %s from list '%s'." % (items_str, locator))
221+
222+
select = self._get_select_list(locator)
223+
for label in labels:
224+
select.select_by_visible_text(label)
225+
185226
def unselect_from_list(self, locator, *items):
186227
"""Unselects given values from select list identified by locator.
187228
188229
As a special case, giving empty list as `*items` will remove all
189230
selections.
190231
232+
*items try to unselect by value AND by label.
233+
234+
It's faster to use 'by index/value/label' functions.
235+
191236
Select list keywords work on both lists and combo boxes. Key attributes for
192237
select lists are `id` and `name`. See `introduction` for details about
193238
locating elements.
194239
"""
195240
items_str = items and "option(s) '%s'" % ", ".join(items) or "all options"
196241
self._info("Unselecting %s from list '%s'." % (items_str, locator))
197-
items = list(items)
198242

199243
select = self._get_select_list(locator)
200244
if not select.is_multiple:
@@ -205,18 +249,70 @@ def unselect_from_list(self, locator, *items):
205249
return
206250

207251
select, options = self._get_select_list_options(select)
208-
option_values = self._get_values_for_options(options)
209-
option_labels = self._get_labels_for_options(options)
210252
for item in items:
211-
option_index = None
212-
try: option_index = option_values.index(item)
213-
except:
214-
try: option_index = option_labels.index(item)
215-
except: continue
216-
select.deselect_by_index(option_index)
253+
select.deselect_by_value(item)
254+
select.deselect_by_visible_text(item)
255+
256+
def unselect_from_list_by_index(self, locator, *indexes):
257+
"""Unselects `*indexes` from list identified by `locator`
258+
259+
Select list keywords work on both lists and combo boxes. Key attributes for
260+
select lists are `id` and `name`. See `introduction` for details about
261+
locating elements.
262+
"""
263+
if not indexes:
264+
raise ValueError("No index given.")
265+
266+
items_str = "index(es) '%s'" % ", ".join(indexes)
267+
self._info("Unselecting %s from list '%s'." % (items_str, locator))
268+
269+
select = self._get_select_list(locator)
270+
if not select.is_multiple:
271+
raise RuntimeError("Keyword 'Unselect from list' works only for multiselect lists.")
272+
273+
for index in indexes:
274+
select.deselect_by_index(int(index))
275+
276+
def unselect_from_list_by_value(self, locator, *values):
277+
"""Unselects `*values` from list identified by `locator`
278+
279+
Select list keywords work on both lists and combo boxes. Key attributes for
280+
select lists are `id` and `name`. See `introduction` for details about
281+
locating elements.
282+
"""
283+
if not values:
284+
raise ValueError("No value given.")
285+
items_str = "value(s) '%s'" % ", ".join(values)
286+
self._info("Unselecting %s from list '%s'." % (items_str, locator))
287+
288+
select = self._get_select_list(locator)
289+
if not select.is_multiple:
290+
raise RuntimeError("Keyword 'Unselect from list' works only for multiselect lists.")
291+
292+
for value in values:
293+
select.deselect_by_value(value)
294+
295+
def unselect_from_list_by_label(self, locator, *labels):
296+
"""Unselects `*labels` from list identified by `locator`
297+
298+
Select list keywords work on both lists and combo boxes. Key attributes for
299+
select lists are `id` and `name`. See `introduction` for details about
300+
locating elements.
301+
"""
302+
if not labels:
303+
raise ValueError("No value given.")
304+
items_str = "label(s) '%s'" % ", ".join(labels)
305+
self._info("Unselecting %s from list '%s'." % (items_str, locator))
306+
307+
select = self._get_select_list(locator)
308+
if not select.is_multiple:
309+
raise RuntimeError("Keyword 'Unselect from list' works only for multiselect lists.")
310+
311+
for label in labels:
312+
select.deselect_by_visible_text(label)
217313

218314
# Private
219-
315+
220316
def _get_labels_for_options(self, options):
221317
labels = []
222318
for option in options:
@@ -238,7 +334,7 @@ def _get_select_list_options_selected(self, locator):
238334
select = self._get_select_list(locator)
239335
# TODO: Handle possible exception thrown by all_selected_options
240336
return select, select.all_selected_options
241-
337+
242338
def _get_values_for_options(self, options):
243339
values = []
244340
for option in options:

0 commit comments

Comments
 (0)