From 0f71c39329751ade655e4510bc7ab414ff48a21a Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Tue, 27 Aug 2013 12:09:57 -0400 Subject: [PATCH 1/5] Added code and tests to check for non-existing select items from list @adwu73 submitted a pull request, #92, with changes to perform this check but we had delayed pulling it and in the meantime other chnages were made. I am resubmitting similar changes working out the best way to catch and report the error. --- .../keywords/_selectelement.py | 22 ++++++++++++++++--- test/acceptance/keywords/lists.txt | 8 +++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Selenium2Library/keywords/_selectelement.py b/src/Selenium2Library/keywords/_selectelement.py index 1cc6bdf66..d6de6d941 100644 --- a/src/Selenium2Library/keywords/_selectelement.py +++ b/src/Selenium2Library/keywords/_selectelement.py @@ -159,6 +159,10 @@ def select_from_list(self, locator, *items): select lists are `id` and `name`. See `introduction` for details about locating elements. """ + #import pdb,sys; pdb.Pdb(stdout=sys.__stdout__).set_trace() + #non_existing_item = True + 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 +174,22 @@ def select_from_list(self, locator, *items): return for item in items: - try: select.select_by_value(item) + try: + select.select_by_value(item) + #non_existing_item = False except: - try: select.select_by_visible_text(item) - except: continue + try: + select.select_by_visible_text(item) + #non_existing_item = False + except: + non_existing_items = non_existing_items + [item] + continue + + #if non_existing_item: + # raise ValueError("No index given.") + if any(non_existing_items): + #raise ValueError("%s not in list '%s'." % (non_existing_items, locator)) + raise ValueError("%s not in list '%s'." % (", ".join(non_existing_items), 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..8abb53a7e 100644 --- a/test/acceptance/keywords/lists.txt +++ b/test/acceptance/keywords/lists.txt @@ -73,6 +73,14 @@ 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 + 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 + ... NoSuchElementException: Message: u'Could not locate element with visible text: Tin Can Phone'\ \ + ... Select From List By Label preferred_channel Tin Can Phone + 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 From 8d69e9ab16e9338beb1582d2ad5c3eb324349c84 Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Tue, 27 Aug 2013 14:02:15 -0400 Subject: [PATCH 2/5] Added test for unselect non-existing items. --- test/acceptance/keywords/lists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/acceptance/keywords/lists.txt b/test/acceptance/keywords/lists.txt index 8abb53a7e..efcd998c4 100644 --- a/test/acceptance/keywords/lists.txt +++ b/test/acceptance/keywords/lists.txt @@ -81,6 +81,15 @@ Select Non-Existing Item From Single Selection List ... NoSuchElementException: Message: u'Could not locate element with visible text: Tin Can Phone'\ \ ... Select From List By Label preferred_channel Tin Can Phone +Unselect Non-Existing Item From List + [Tags] OnlyThisOne + Run Keyword And Expect Error + ... * + ... Unselect From List possible_channels Tin Can Phone Smoke Signals + Run Keyword And Expect Error + ... * + ... 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 From f1411972ac94bd496c182f77e7e35e9f876ea140 Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Tue, 27 Aug 2013 16:29:25 -0400 Subject: [PATCH 3/5] Updated expected error for select test. --- test/acceptance/keywords/lists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/acceptance/keywords/lists.txt b/test/acceptance/keywords/lists.txt index efcd998c4..2369398f4 100644 --- a/test/acceptance/keywords/lists.txt +++ b/test/acceptance/keywords/lists.txt @@ -75,8 +75,12 @@ Select From Single Selection List Select Non-Existing Item From Single Selection List [Tags] OnlyThisOne - 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: Tin Can Phone, Smoke Signals not in list 'preferred_channel'. + ... Select From List preferred_channel Tin Can Phone Smoke Signals + Run Keyword And Expect Error + ... ValueError: Tin Can Phone, Smoke Signals not in list 'preferred_channel'. + ... Select From List preferred_channel Tin Can Phone Smoke Signals Email 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 From de7358a74719e9cd9ee8b147d15ddd0377915948 Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Sun, 1 Sep 2013 14:59:36 -0400 Subject: [PATCH 4/5] Updated code and tests to check for non-existing select items from list Now check to see if select list is multi-selection or not. If it is multi- select then raise error for any non-existing item(s). If it is a single select list then log warning if any item, up to the second to last item, that does not exist within the select list. And if the last item does not exist then a ValueError is thrown. This discerning error/warning reporting for single select list was designed to accommodate current stated operation of the keyword, "If more than one value is given for a single-selection list, the last value will be selected." --- .../keywords/_selectelement.py | 16 ++++++------ test/acceptance/keywords/lists.txt | 26 +++++++++++++------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Selenium2Library/keywords/_selectelement.py b/src/Selenium2Library/keywords/_selectelement.py index d6de6d941..181ce930b 100644 --- a/src/Selenium2Library/keywords/_selectelement.py +++ b/src/Selenium2Library/keywords/_selectelement.py @@ -159,8 +159,6 @@ def select_from_list(self, locator, *items): select lists are `id` and `name`. See `introduction` for details about locating elements. """ - #import pdb,sys; pdb.Pdb(stdout=sys.__stdout__).set_trace() - #non_existing_item = True non_existing_items = [] items_str = items and "option(s) '%s'" % ", ".join(items) or "all options" @@ -176,20 +174,22 @@ def select_from_list(self, locator, *items): for item in items: try: select.select_by_value(item) - #non_existing_item = False except: try: select.select_by_visible_text(item) - #non_existing_item = False except: non_existing_items = non_existing_items + [item] continue - #if non_existing_item: - # raise ValueError("No index given.") if any(non_existing_items): - #raise ValueError("%s not in list '%s'." % (non_existing_items, locator)) - raise ValueError("%s not in list '%s'." % (", ".join(non_existing_items), locator)) + 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 2369398f4..83dc793a2 100644 --- a/test/acceptance/keywords/lists.txt +++ b/test/acceptance/keywords/lists.txt @@ -76,23 +76,33 @@ Select From Single Selection List Select Non-Existing Item From Single Selection List [Tags] OnlyThisOne Run Keyword And Expect Error - ... ValueError: Tin Can Phone, Smoke Signals not in list 'preferred_channel'. + ... 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: Tin Can Phone, Smoke Signals not in list 'preferred_channel'. - ... Select From List preferred_channel Tin Can Phone Smoke Signals Email + ... 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 -Unselect Non-Existing Item From List +Select Non-Existing Item From Multi-Selection List [Tags] OnlyThisOne Run Keyword And Expect Error - ... * - ... Unselect From List possible_channels Tin Can Phone Smoke Signals + ... 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 - ... * - ... Unselect From List possible_channels Tin Can Phone Smoke Signals Email + ... 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'. From f179966fe716bef20d41a254e3dfbe201799dcda Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Sun, 1 Sep 2013 16:32:49 -0400 Subject: [PATCH 5/5] Updated documentation for non-existing select items from list. Added information about exception for non-existing items. Also updated CHANGES.rst. --- CHANGES.rst | 7 ++++++- src/Selenium2Library/keywords/_selectelement.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 181ce930b..e63bfd466 100644 --- a/src/Selenium2Library/keywords/_selectelement.py +++ b/src/Selenium2Library/keywords/_selectelement.py @@ -155,6 +155,11 @@ 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.