diff --git a/CHANGES.rst b/CHANGES.rst index 3cce3dc6f..6dba99e70 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 --- diff --git a/src/Selenium2Library/keywords/_selectelement.py b/src/Selenium2Library/keywords/_selectelement.py index 1cc6bdf66..e63bfd466 100644 --- a/src/Selenium2Library/keywords/_selectelement.py +++ b/src/Selenium2Library/keywords/_selectelement.py @@ -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)) @@ -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` diff --git a/test/acceptance/keywords/lists.txt b/test/acceptance/keywords/lists.txt index fcbda8dce..83dc793a2 100644 --- a/test/acceptance/keywords/lists.txt +++ b/test/acceptance/keywords/lists.txt @@ -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