From b66cf91f9ad37d2b45ef1f72c3970d4762d0c655 Mon Sep 17 00:00:00 2001 From: divfor Date: Tue, 12 May 2015 12:17:42 +0800 Subject: [PATCH 01/10] enhance select window --- .../keywords/_browsermanagement.py | 10 +++++- .../locators/windowmanager.py | 32 ++++++++++++------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 57b673edf..13761b7c8 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -290,6 +290,7 @@ def select_frame(self, locator): def select_window(self, locator=None): """Selects the window found with `locator` as the context of actions. + Return value is ether current window handle before select action or None. If the window is found, all subsequent commands use that window, until this keyword is used again. If the window is not found, this keyword fails. @@ -299,7 +300,10 @@ def select_window(self, locator=None): javascript name of the window. If multiple windows with same identifier are found, the first one is selected. - Special locator `main` (default) can be used to select the main window. + Special locator `main` (default) used to select the main window and returns from-window handle. + Special locator `new` switches to new opened window and returns old window handle. + Special locator `current` does not switch window but return current window handle. + The returned window handle could be used as locator to switch back to that window. It is also possible to specify the approach Selenium2Library should take to find a window by specifying a locator strategy: @@ -315,7 +319,11 @@ def select_window(self, locator=None): | Title Should Be | Popup Title | | Select Window | | | # Chooses the main window again | """ + try: + from_handle = self._current_browser().get_current_window_handle() + except NoSuchWindowException: pass self._window_manager.select(self._current_browser(), locator) + return from_handle if from_handle else None def unselect_frame(self): """Sets the top frame as the current frame.""" diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index c03fac8eb..a20eec12e 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -52,21 +52,29 @@ def _select_by_url(self, browser, criteria): "Unable to locate window with URL '" + criteria + "'") def _select_by_default(self, browser, criteria): + if criteria.lower() == "current": + return + handles = browser.get_window_handles() if criteria is None or len(criteria) == 0 or criteria.lower() == "null": - browser.switch_to_window('') + browser.switch_to_window(handles[0]) return - - try: - self._select_by_name(browser, criteria) + if criteria.lower() == "new" or criteria.lower() == "popup": + try: + start_handle = browser.get_current_window_handle() + except NoSuchWindowException: + raise AssertionError("No current window. where are you to make a popup window?") + if len(handles) < 2 or handles[-1] == start_handle: + raise AssertionError("No new window found to switch to") + browser.switch_to_window(handles[-1]) return - except ValueError: pass - - try: - self._select_by_title(browser, criteria) - return - except ValueError: pass - - raise ValueError("Unable to locate window with name or title '" + criteria + "'") + for handle in handles: + browser.switch_to_window(handle) + if criteria == handle: + return + for item in browser.get_current_window_info()[2:4]: + if item.strip().lower() == criteria.lower(): + return + raise ValueError("Unable to locate window with handle or name or title or URL '" + criteria + "'") # Private From 921c849a1a87bb02e584c2c80c7e4989dc2a557d Mon Sep 17 00:00:00 2001 From: divfor Date: Wed, 13 May 2015 00:44:21 +0800 Subject: [PATCH 02/10] add code to enhance Select Window There are some special locators for searching target window: string 'main' (default): select the main window; string 'current': just return current window handle; a single window handle: directly select window by handle; string 'new': select new opened window assuming it is last-position indexed (no iframe) a past list of window handles: select new window by excluding past window handle list See 'Get Window Handles' to get the past list of window handles --- .../keywords/_browsermanagement.py | 20 +++++++++++++------ .../locators/windowmanager.py | 8 ++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 13761b7c8..7c0951abb 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -290,8 +290,10 @@ def select_frame(self, locator): def select_window(self, locator=None): """Selects the window found with `locator` as the context of actions. - Return value is ether current window handle before select action or None. - + locator value may be name, title, special string or window handle(s). + return value is current window handle if it exists before select else None. + The returned window handle could be used as locator to switch back to that window. + If the window is found, all subsequent commands use that window, until this keyword is used again. If the window is not found, this keyword fails. @@ -300,10 +302,13 @@ def select_window(self, locator=None): javascript name of the window. If multiple windows with same identifier are found, the first one is selected. - Special locator `main` (default) used to select the main window and returns from-window handle. - Special locator `new` switches to new opened window and returns old window handle. - Special locator `current` does not switch window but return current window handle. - The returned window handle could be used as locator to switch back to that window. + There are some special locators for searching target window: + string 'main' (default): select the main window; + string 'current': just return current window handle; + a single window handle: directly select window by handle; + string 'new': select new opened window assuming it is last-position indexed (no iframe) + a past list of window handles: select new window by excluding past window handle list + See 'Get Window Handles' to get the past list of window handles It is also possible to specify the approach Selenium2Library should take to find a window by specifying a locator strategy: @@ -325,6 +330,9 @@ def select_window(self, locator=None): self._window_manager.select(self._current_browser(), locator) return from_handle if from_handle else None + def get_window_handles(): + return self._current_browser().get_window_handles() + def unselect_frame(self): """Sets the top frame as the current frame.""" self._current_browser().switch_to_default_content() diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index a20eec12e..e73d64303 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -55,6 +55,12 @@ def _select_by_default(self, browser, criteria): if criteria.lower() == "current": return handles = browser.get_window_handles() + if type(criteria) == list: + for handle in handles: + if handle not in criteria: + browser.switch_to_window(handle) + return + raise ValueError("Unable to locate new window") if criteria is None or len(criteria) == 0 or criteria.lower() == "null": browser.switch_to_window(handles[0]) return @@ -81,6 +87,8 @@ def _select_by_default(self, browser, criteria): def _parse_locator(self, locator): prefix = None criteria = locator + if type(locator) == list: + return (prefix, criteria) if locator is not None and len(locator) > 0: locator_parts = locator.partition('=') if len(locator_parts[1]) > 0: From a5d1b113f7314729186f7cafd665a4325bfbb980 Mon Sep 17 00:00:00 2001 From: divfor Date: Wed, 13 May 2015 18:07:52 +0800 Subject: [PATCH 03/10] fix after test add: from selenium.common.exceptions import NoSuchWindowException --- .../keywords/_browsermanagement.py | 31 ++++++++++++------- .../locators/windowmanager.py | 9 ++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 7c0951abb..4b481a677 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -6,6 +6,7 @@ from Selenium2Library.utils import BrowserCache from Selenium2Library.locators import WindowManager from keywordgroup import KeywordGroup +from selenium.common.exceptions import NoSuchWindowException ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) FIREFOX_PROFILE_DIR = os.path.join(ROOT_DIR, 'resources', 'firefoxprofile') @@ -289,10 +290,10 @@ def select_frame(self, locator): self._current_browser().switch_to_frame(element) def select_window(self, locator=None): - """Selects the window found with `locator` as the context of actions. - locator value may be name, title, special string or window handle(s). - return value is current window handle if it exists before select else None. - The returned window handle could be used as locator to switch back to that window. + """Selects the window matching locator and return previous window handle. + + locator: any of name, title, url, window handle, excluded handle's list, or special words. + return: either current window handle before selecting, or None if no current window. If the window is found, all subsequent commands use that window, until this keyword is used again. If the window is not found, this keyword fails. @@ -304,11 +305,10 @@ def select_window(self, locator=None): There are some special locators for searching target window: string 'main' (default): select the main window; - string 'current': just return current window handle; - a single window handle: directly select window by handle; + string 'current': only return current window handle; string 'new': select new opened window assuming it is last-position indexed (no iframe) - a past list of window handles: select new window by excluding past window handle list - See 'Get Window Handles' to get the past list of window handles + excluded handle's list: select the first window not in the list + See 'List Windows' to get window handle list It is also possible to specify the approach Selenium2Library should take to find a window by specifying a locator strategy: @@ -326,11 +326,20 @@ def select_window(self, locator=None): """ try: from_handle = self._current_browser().get_current_window_handle() - except NoSuchWindowException: pass + except NoSuchWindowException: + from_handle = None self._window_manager.select(self._current_browser(), locator) - return from_handle if from_handle else None + return from_handle + + def close_window_and_switch_to(self, locator=None): + """Closes current window and then switch to the window matching given locator. + See 'Select Window' keyword for same locator requirement + """ + self._current_browser().close() + self.select_window(locator) - def get_window_handles(): + def list_windows(): + """Return all current window handles as a list""" return self._current_browser().get_window_handles() def unselect_frame(self): diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index e73d64303..c925fad62 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -68,7 +68,7 @@ def _select_by_default(self, browser, criteria): try: start_handle = browser.get_current_window_handle() except NoSuchWindowException: - raise AssertionError("No current window. where are you to make a popup window?") + raise AssertionError("No current window. where are you making a popup window?") if len(handles) < 2 or handles[-1] == start_handle: raise AssertionError("No new window found to switch to") browser.switch_to_window(handles[-1]) @@ -111,10 +111,13 @@ def _get_window_infos(self, browser): return window_infos def _select_matching(self, browser, matcher, error): - starting_handle = browser.get_current_window_handle() + try: + starting_handle = browser.get_current_window_handle() + except NoSuchWindowException: pass for handle in browser.get_window_handles(): browser.switch_to_window(handle) if matcher(browser.get_current_window_info()): return - browser.switch_to_window(starting_handle) + if starting_handle: + browser.switch_to_window(starting_handle) raise ValueError(error) From 52556f8453e7532fd82be1800093023c4fd6282b Mon Sep 17 00:00:00 2001 From: divfor Date: Wed, 13 May 2015 23:49:28 +0800 Subject: [PATCH 04/10] fix minor bug change current to self, fix exception proceeding --- src/Selenium2Library/locators/windowmanager.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index c925fad62..1fa836ef2 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -52,7 +52,7 @@ def _select_by_url(self, browser, criteria): "Unable to locate window with URL '" + criteria + "'") def _select_by_default(self, browser, criteria): - if criteria.lower() == "current": + if criteria.lower() == "self": return handles = browser.get_window_handles() if type(criteria) == list: @@ -101,19 +101,20 @@ def _parse_locator(self, locator): def _get_window_infos(self, browser): window_infos = [] - starting_handle = browser.get_current_window_handle() + start_handle = browser.get_current_window_handle() try: for handle in browser.get_window_handles(): browser.switch_to_window(handle) window_infos.append(browser.get_current_window_info()) finally: - browser.switch_to_window(starting_handle) + browser.switch_to_window(start_handle) return window_infos def _select_matching(self, browser, matcher, error): try: starting_handle = browser.get_current_window_handle() - except NoSuchWindowException: pass + except NoSuchWindowException: + starting_handle = None for handle in browser.get_window_handles(): browser.switch_to_window(handle) if matcher(browser.get_current_window_info()): From b15c82babfc1decd13bc30ff582f0dbe6411c5fa Mon Sep 17 00:00:00 2001 From: divfor Date: Thu, 14 May 2015 15:54:27 +0800 Subject: [PATCH 05/10] code complete for new select window enable: select by handle, list all handles, select new by excluding old handle list, close window and select --- .../keywords/_browsermanagement.py | 8 +++--- .../locators/windowmanager.py | 26 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 4b481a677..083497e88 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -305,9 +305,9 @@ def select_window(self, locator=None): There are some special locators for searching target window: string 'main' (default): select the main window; - string 'current': only return current window handle; - string 'new': select new opened window assuming it is last-position indexed (no iframe) - excluded handle's list: select the first window not in the list + string 'self': only return current window handle; + string 'new': select the last-indexed window assuming it is the newest opened window + list of window handle: select the first window not in the list See 'List Windows' to get window handle list It is also possible to specify the approach Selenium2Library should take @@ -331,7 +331,7 @@ def select_window(self, locator=None): self._window_manager.select(self._current_browser(), locator) return from_handle - def close_window_and_switch_to(self, locator=None): + def close_window_and_select(self, locator=None): """Closes current window and then switch to the window matching given locator. See 'Select Window' keyword for same locator requirement """ diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index 1fa836ef2..bee05ff60 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -52,28 +52,28 @@ def _select_by_url(self, browser, criteria): "Unable to locate window with URL '" + criteria + "'") def _select_by_default(self, browser, criteria): - if criteria.lower() == "self": - return - handles = browser.get_window_handles() if type(criteria) == list: - for handle in handles: + for handle in browser.get_window_handles(): if handle not in criteria: browser.switch_to_window(handle) return raise ValueError("Unable to locate new window") + if criteria.lower() == "self": + return if criteria is None or len(criteria) == 0 or criteria.lower() == "null": - browser.switch_to_window(handles[0]) + browser.switch_to_window(browser.get_window_handles()[0]) return if criteria.lower() == "new" or criteria.lower() == "popup": try: start_handle = browser.get_current_window_handle() except NoSuchWindowException: raise AssertionError("No current window. where are you making a popup window?") + handles = browser.get_window_handles() if len(handles) < 2 or handles[-1] == start_handle: - raise AssertionError("No new window found to switch to") + raise AssertionError("No new window found to select") browser.switch_to_window(handles[-1]) return - for handle in handles: + for handle in browser.get_window_handles(): browser.switch_to_window(handle) if criteria == handle: return @@ -101,24 +101,20 @@ def _parse_locator(self, locator): def _get_window_infos(self, browser): window_infos = [] - start_handle = browser.get_current_window_handle() + starting_handle = browser.get_current_window_handle() try: for handle in browser.get_window_handles(): browser.switch_to_window(handle) window_infos.append(browser.get_current_window_info()) finally: - browser.switch_to_window(start_handle) + browser.switch_to_window(starting_handle) return window_infos def _select_matching(self, browser, matcher, error): - try: - starting_handle = browser.get_current_window_handle() - except NoSuchWindowException: - starting_handle = None + starting_handle = browser.get_current_window_handle() for handle in browser.get_window_handles(): browser.switch_to_window(handle) if matcher(browser.get_current_window_info()): return - if starting_handle: - browser.switch_to_window(starting_handle) + browser.switch_to_window(starting_handle) raise ValueError(error) From 4e237c017953f142c224f902b747bc6a98a36c1a Mon Sep 17 00:00:00 2001 From: divfor Date: Thu, 14 May 2015 16:24:36 +0800 Subject: [PATCH 06/10] add parameter self for list_windows() --- src/Selenium2Library/keywords/_browsermanagement.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 083497e88..062b2dbab 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -338,7 +338,7 @@ def close_window_and_select(self, locator=None): self._current_browser().close() self.select_window(locator) - def list_windows(): + def list_windows(self): """Return all current window handles as a list""" return self._current_browser().get_window_handles() From 36e00c4a25d480310fdb2e1c01909fb46e3d82c6 Mon Sep 17 00:00:00 2001 From: divfor Date: Sat, 16 May 2015 20:37:06 +0800 Subject: [PATCH 07/10] add test cases --- .../keywords/_browsermanagement.py | 10 +++--- .../locators/windowmanager.py | 4 +-- test/acceptance/windows.txt | 34 ++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Selenium2Library/keywords/_browsermanagement.py b/src/Selenium2Library/keywords/_browsermanagement.py index 062b2dbab..e2740ca93 100644 --- a/src/Selenium2Library/keywords/_browsermanagement.py +++ b/src/Selenium2Library/keywords/_browsermanagement.py @@ -325,18 +325,18 @@ def select_window(self, locator=None): | Select Window | | | # Chooses the main window again | """ try: - from_handle = self._current_browser().get_current_window_handle() + return self._current_browser().get_current_window_handle() except NoSuchWindowException: - from_handle = None - self._window_manager.select(self._current_browser(), locator) - return from_handle + pass + finally: + self._window_manager.select(self._current_browser(), locator) def close_window_and_select(self, locator=None): """Closes current window and then switch to the window matching given locator. See 'Select Window' keyword for same locator requirement """ self._current_browser().close() - self.select_window(locator) + return self.select_window(locator) def list_windows(self): """Return all current window handles as a list""" diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index bee05ff60..657253a12 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -52,7 +52,7 @@ def _select_by_url(self, browser, criteria): "Unable to locate window with URL '" + criteria + "'") def _select_by_default(self, browser, criteria): - if type(criteria) == list: + if isinstance(criteria, list): for handle in browser.get_window_handles(): if handle not in criteria: browser.switch_to_window(handle) @@ -67,7 +67,7 @@ def _select_by_default(self, browser, criteria): try: start_handle = browser.get_current_window_handle() except NoSuchWindowException: - raise AssertionError("No current window. where are you making a popup window?") + raise AssertionError("Currently no window focus. where are you making a popup window?") handles = browser.get_window_handles() if len(handles) < 2 or handles[-1] == start_handle: raise AssertionError("No new window found to select") diff --git a/test/acceptance/windows.txt b/test/acceptance/windows.txt index 552d3754d..874793834 100644 --- a/test/acceptance/windows.txt +++ b/test/acceptance/windows.txt @@ -41,7 +41,6 @@ Get and Set Window Size Should Be Equal ${returned_width} ${win_width} Should Be Equal ${returned_height} ${win_height} - Get and Set Window Position ${position_x}= Set Variable ${100} ${position_y}= Set Variable ${100} @@ -50,6 +49,39 @@ Get and Set Window Position Should Be Equal ${position_x} ${returned_x} Should Be Equal ${position_y} ${returned_y} +Select Window By Handle + Cannot Be Executed in IE + Click Link my popup + ${parent}= Select Window Original + Title Should Be Original + ${child}= Select Window ${parent} + Title Should Be Click link to show a popup window + Select Window ${child} + Close Window + ${FromWindow}= Select Window ${parent} + Title Should Be Click link to show a popup window + Should Be True ${FromWindow} == None + +Select Popup Window By Excluded List + Cannot Be Executed in IE + @{excluded_handle_list}= List Windows + Click Link my popup + ${parent}= Select Window ${excluded_handle_list} + Title Should Be Original + Close Window + Select Window ${parent} + Title Should Be Click link to show a popup window + +Select Window By Special Locator + Cannot Be Executed in IE + ${start}= Select Window self + Click Link my popup + ${parent}= Select Window new + Title Should Be Original + Should Be True '${start}' == '${parent}' + Close Window And Select main + Title Should Be Click link to show a popup window + *Keywords* Open Popup Window, Select It And Verify From 0e93de39841b648c50bb52295894b1afc0cd847e Mon Sep 17 00:00:00 2001 From: divfor Date: Sat, 16 May 2015 21:12:29 +0800 Subject: [PATCH 08/10] change type() to isinstance() --- src/Selenium2Library/locators/windowmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index 657253a12..0084b807a 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -87,7 +87,7 @@ def _select_by_default(self, browser, criteria): def _parse_locator(self, locator): prefix = None criteria = locator - if type(locator) == list: + if isinstance(locator, list): return (prefix, criteria) if locator is not None and len(locator) > 0: locator_parts = locator.partition('=') From 6e693f10bd3048e4016760ec37c44a3a52607966 Mon Sep 17 00:00:00 2001 From: divfor Date: Sat, 16 May 2015 21:37:34 +0800 Subject: [PATCH 09/10] updated changelog --- CHANGES.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index f2c8e78be..a2292f79e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,14 @@ Release Notes 1.7 (unreleased) ---------------- +- Added 'Close Window And Select' to simplify 'Close Window' + 'Select Window'. + Added 'List Windows' to return a list of all current handles. + Enhanced 'Select Window' to select new window by excluding a list of window handles + (the strict way), or by special locator 'new' (the simplified but less strict way) + Enhanced 'Select Window' to accept window handle as locator, and special locator + 'self' to return current window handle. + [divfor] + - Corrected error message in new keyword 'Wait Until Element Is Not Visible' to reflect element being visible instead of not visible. [joepurdy] From 6d2672030d71ecfe9cf5fa84211ba0d745720bf9 Mon Sep 17 00:00:00 2001 From: divfor Date: Thu, 21 May 2015 00:01:48 +0800 Subject: [PATCH 10/10] re-org code for making more clear --- .../locators/windowmanager.py | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/Selenium2Library/locators/windowmanager.py b/src/Selenium2Library/locators/windowmanager.py index 0084b807a..9b6259742 100644 --- a/src/Selenium2Library/locators/windowmanager.py +++ b/src/Selenium2Library/locators/windowmanager.py @@ -24,7 +24,14 @@ def get_window_titles(self, browser): def select(self, browser, locator): assert browser is not None - + if isinstance(locator, list): + self._select_by_excludes(browser, locator) + return + if locator.lower() == "self" or locator.lower() == "current": + return + if locator.lower() == "new" or locator.lower() == "popup": + self._select_by_last_index(browser) + return (prefix, criteria) = self._parse_locator(locator) strategy = self._strategies.get(prefix) if strategy is None: @@ -52,27 +59,9 @@ def _select_by_url(self, browser, criteria): "Unable to locate window with URL '" + criteria + "'") def _select_by_default(self, browser, criteria): - if isinstance(criteria, list): - for handle in browser.get_window_handles(): - if handle not in criteria: - browser.switch_to_window(handle) - return - raise ValueError("Unable to locate new window") - if criteria.lower() == "self": - return if criteria is None or len(criteria) == 0 or criteria.lower() == "null": browser.switch_to_window(browser.get_window_handles()[0]) return - if criteria.lower() == "new" or criteria.lower() == "popup": - try: - start_handle = browser.get_current_window_handle() - except NoSuchWindowException: - raise AssertionError("Currently no window focus. where are you making a popup window?") - handles = browser.get_window_handles() - if len(handles) < 2 or handles[-1] == start_handle: - raise AssertionError("No new window found to select") - browser.switch_to_window(handles[-1]) - return for handle in browser.get_window_handles(): browser.switch_to_window(handle) if criteria == handle: @@ -82,13 +71,29 @@ def _select_by_default(self, browser, criteria): return raise ValueError("Unable to locate window with handle or name or title or URL '" + criteria + "'") + def _select_by_last_index(self, browser): + handles = browser.get_window_handles() + try: + if handles[-1] == browser.get_current_window_handle(): + raise AssertionError("Unable to get new window from last index. Please use '@{ex}= | List Windows' + new window trigger + 'Select Window | ${ex}'") + except IndexError: + raise AssertionError("No window found") + except NoSuchWindowException: + raise AssertionError("Currently no focus window. where are you making a popup window?") + browser.switch_to_window(handles[-1]) + + def _select_by_excludes(self, browser, excludes): + for handle in browser.get_window_handles(): + if handle not in excludes: + browser.switch_to_window(handle) + return + raise ValueError("Unable to locate new window") + # Private def _parse_locator(self, locator): prefix = None criteria = locator - if isinstance(locator, list): - return (prefix, criteria) if locator is not None and len(locator) > 0: locator_parts = locator.partition('=') if len(locator_parts[1]) > 0: