Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ Release Notes

1.4 (unreleased)
-----------------
- Raise exception in selecting non-existing item in list. Error handling varies
between single-select and multi-select lists. See keyword documentation for
more information.
[adwu73][emanlove]

- Added 'Get Window Size' and 'Set Window Size' keywords matching the
Selenium functionality.
[emanlove] [ombre42]
[emanlove][ombre42]

1.3
---
Expand Down
27 changes: 24 additions & 3 deletions src/Selenium2Library/keywords/_selectelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,17 @@ def select_from_list(self, locator, *items):

It's faster to use 'by index/value/label' functions.

An exception is raised for a single-selection list if the last
value does not exist in the list and a warning for all other non-
existing items. For a multi-selection list, an exception is raised
for any and all non-existing values.

Select list keywords work on both lists and combo boxes. Key attributes for
select lists are `id` and `name`. See `introduction` for details about
locating elements.
"""
non_existing_items = []

items_str = items and "option(s) '%s'" % ", ".join(items) or "all options"
self._info("Selecting %s from list '%s'." % (items_str, locator))

Expand All @@ -170,10 +177,24 @@ def select_from_list(self, locator, *items):
return

for item in items:
try: select.select_by_value(item)
try:
select.select_by_value(item)
except:
try: select.select_by_visible_text(item)
except: continue
try:
select.select_by_visible_text(item)
except:
non_existing_items = non_existing_items + [item]
continue

if any(non_existing_items):
if select.is_multiple:
raise ValueError("Options '%s' not in list '%s'." % (", ".join(non_existing_items), locator))
else:
if any (non_existing_items[:-1]):
items_str = non_existing_items[:-1] and "Option(s) '%s'" % ", ".join(non_existing_items[:-1])
self._warn("%s not found within list '%s'." % (items_str, locator))
if items and items[-1] in non_existing_items:
raise ValueError("Option '%s' not in list '%s'." % (items[-1], locator))

def select_from_list_by_index(self, locator, *indexes):
"""Selects `*indexes` from list identified by `locator`
Expand Down
31 changes: 31 additions & 0 deletions test/acceptance/keywords/lists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ Select From Single Selection List
Select From List preferred_channel
List Selection Should Be preferred_channel Direct mail

Select Non-Existing Item From Single Selection List
[Tags] OnlyThisOne
Run Keyword And Expect Error
... ValueError: Option 'Smoke Signals' not in list 'preferred_channel'.
... Select From List preferred_channel Tin Can Phone Smoke Signals
Select From List preferred_channel Tin Can Phone Smoke Signals Email
Run Keyword And Expect Error
... ValueError: Option 'Tin Can Phone' not in list 'preferred_channel'.
... Select From List preferred_channel Smoke Signals Email Tin Can Phone
Run Keyword And Expect Error
... NoSuchElementException: Message: u'Could not locate element with visible text: Tin Can Phone'\ \
... Select From List By Label preferred_channel Tin Can Phone

Select Non-Existing Item From Multi-Selection List
[Tags] OnlyThisOne
Run Keyword And Expect Error
... ValueError: Options 'Tin Can Phone, Smoke Signals' not in list 'possible_channels'.
... Select From List possible_channels Tin Can Phone Smoke Signals
Run Keyword And Expect Error
... ValueError: Options 'Tin Can Phone, Smoke Signals' not in list 'possible_channels'.
... Select From List possible_channels Tin Can Phone Smoke Signals Email
Run Keyword And Expect Error
... ValueError: Options 'Tin Can Phone, Smoke Signals' not in list 'possible_channels'.
... Select From List possible_channels Tin Can Phone Email Smoke Signals

Unselect Non-Existing Item From List
[Documentation] LOG 5 Unselecting non-existing items will not throw an error.
[Tags] OnlyThisOne
Unselect From List possible_channels Tin Can Phone Smoke Signals
Unselect From List possible_channels Tin Can Phone Smoke Signals Email

Select From Multiselect List
[Documentation] LOG 5 Selecting option(s) 'Direct mail, phone' from list 'possible_channels'.
Select And verify selection possible_channels email email Telephone
Expand Down