Skip to content

Commit 51e2c2d

Browse files
pekkaklarckaaltat
authored andcommitted
Cleanup selecting windows. (robotframework#968)
* Cleanup selecting windows. * Contains code and test changes related to robotframework#966 * Get Window Handles to replace List Windows. See robotframework#966. * Documented cleaned up Select Window. * Fixes robotframework#966. Documentation cleanup also related to robotframework#925. * Moved window keywords to own component
1 parent 909a4ba commit 51e2c2d

8 files changed

Lines changed: 353 additions & 294 deletions

File tree

src/SeleniumLibrary/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
ScreenshotKeywords,
3232
SelectElementKeywords,
3333
TableElementKeywords,
34-
WaitingKeywords)
34+
WaitingKeywords,
35+
WindowKeywords)
3536
from SeleniumLibrary.locators import ElementFinder, TableElementFinder
3637
from SeleniumLibrary.utils import (BrowserCache, Deprecated, LibraryListener,
3738
timestr_to_secs)
@@ -339,7 +340,8 @@ def __init__(self, timeout=5.0, implicit_wait=0.0,
339340
ScreenshotKeywords(self),
340341
SelectElementKeywords(self),
341342
TableElementKeywords(self),
342-
WaitingKeywords(self)
343+
WaitingKeywords(self),
344+
WindowKeywords(self)
343345
]
344346
self._browsers = BrowserCache()
345347
DynamicCore.__init__(self, libraries)

src/SeleniumLibrary/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ class SeleniumLibraryException(Exception):
2121

2222
class ElementNotFound(SeleniumLibraryException):
2323
pass
24+
25+
26+
class WindowNotFound(SeleniumLibraryException):
27+
pass

src/SeleniumLibrary/keywords/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
from .selectelement import SelectElementKeywords
2727
from .tableelement import TableElementKeywords
2828
from .waiting import WaitingKeywords
29+
from .window import WindowKeywords

src/SeleniumLibrary/keywords/browsermanagement.py

Lines changed: 6 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from robot.utils import NormalizedDict
2222
from selenium import webdriver
23-
from selenium.common.exceptions import NoSuchWindowException
2423

2524
from SeleniumLibrary.base import keyword, LibraryComponent
2625
from SeleniumLibrary.locators import WindowManager
@@ -238,150 +237,6 @@ def switch_browser(self, index_or_alias):
238237
self.debug('Switched to browser with Selenium session id %s.'
239238
% self.browser.session_id)
240239

241-
@keyword
242-
def close_window(self):
243-
"""Closes currently opened pop-up window."""
244-
self.browser.close()
245-
246-
@keyword
247-
def get_window_identifiers(self):
248-
"""Returns and logs id attributes of all known browser windows."""
249-
ids = [info.id for info in self._window_manager.get_window_infos()]
250-
return self._log_list(ids)
251-
252-
@keyword
253-
def get_window_names(self):
254-
"""Returns and logs names of all known browser windows."""
255-
names = [info.name for info in self._window_manager.get_window_infos()]
256-
return self._log_list(names)
257-
258-
@keyword
259-
def get_window_titles(self):
260-
"""Returns and logs titles of all known browser windows."""
261-
titles = [info.title for info in self._window_manager.get_window_infos()]
262-
return self._log_list(titles)
263-
264-
@keyword
265-
def maximize_browser_window(self):
266-
"""Maximizes current browser window."""
267-
self.browser.maximize_window()
268-
269-
@keyword
270-
def get_window_size(self):
271-
"""Returns current window width and height as integers.
272-
273-
See also `Set Window Size`.
274-
275-
Example:
276-
| ${width} | ${height}= | `Get Window Size` |
277-
"""
278-
size = self.browser.get_window_size()
279-
return size['width'], size['height']
280-
281-
@keyword
282-
def set_window_size(self, width, height):
283-
"""Sets current windows size to given ``width`` and ``height``.
284-
285-
Values can be given using strings containing numbers or by using
286-
actual numbers. See also `Get Window Size`.
287-
288-
Browsers have a limit how small they can be set. Trying to set them
289-
smaller will cause the actual size to be bigger than the requested
290-
size.
291-
292-
Example:
293-
| `Set Window Size` | 800 | 600 |
294-
"""
295-
return self.browser.set_window_size(int(width), int(height))
296-
297-
@keyword
298-
def get_window_position(self):
299-
"""Returns current window position.
300-
301-
Position is relative to the top left corner of the screen. Returned
302-
values are integers. See also `Set Window Position`.
303-
304-
Example:
305-
| ${x} | ${y}= | `Get Window Position` |
306-
"""
307-
position = self.browser.get_window_position()
308-
return position['x'], position['y']
309-
310-
@keyword
311-
def set_window_position(self, x, y):
312-
"""Sets window position using ``x`` and ``y`` coordinates.
313-
314-
The position is relative to the top left corner of the screen,
315-
but some browsers exclude possible task bar set by the operating
316-
system from the calculation. The actual position may thus be
317-
different with different browsers.
318-
319-
Values can be given using strings containing numbers or by using
320-
actual numbers. See also `Get Window Position`.
321-
322-
Example:
323-
| `Set Window Position` | 100 | 200 |
324-
"""
325-
self.browser.set_window_position(int(x), int(y))
326-
327-
@keyword
328-
def select_window(self, locator=None):
329-
"""Selects the window matching locator and return previous window handle.
330-
331-
locator: any of name, title, url, window handle, excluded handle's list, or special words.
332-
return: either current window handle before selecting, or None if no current window.
333-
334-
If the window is found, all subsequent commands use that window, until
335-
this keyword is used again. If the window is not found, this keyword fails.
336-
337-
By default, when a locator value is provided,
338-
it is matched against the title of the window and the
339-
javascript name of the window. If multiple windows with
340-
same identifier are found, the first one is selected.
341-
342-
There are some special locators for searching target window:
343-
string 'main' (default): select the main window;
344-
string 'self': only return current window handle;
345-
string 'new': select the last-indexed window assuming it is the newest opened window
346-
window list: select the first window not in given list (See 'List Windows' to get the list)
347-
348-
It is also possible to specify the approach SeleniumLibrary should take
349-
to find a window by specifying a locator strategy:
350-
351-
| *Strategy* | *Example* | *Description* |
352-
| title | Select Window `|` title=My Document | Matches by window title |
353-
| name | Select Window `|` name=${name} | Matches by window javascript name |
354-
| url | Select Window `|` url=http://google.com | Matches by window's current URL |
355-
356-
Example:
357-
| Click Link | popup_link | # opens new window |
358-
| Select Window | popupName |
359-
| Title Should Be | Popup Title |
360-
| Select Window | | | # Chooses the main window again |
361-
"""
362-
try:
363-
return self.browser.current_window_handle
364-
except NoSuchWindowException:
365-
pass
366-
finally:
367-
self._window_manager.select(locator)
368-
369-
@keyword
370-
def list_windows(self):
371-
"""Return all current window handles as a list."""
372-
return self.browser.window_handles
373-
374-
@keyword
375-
def get_location(self):
376-
"""Returns the current browser URL."""
377-
return self.browser.current_url
378-
379-
@keyword
380-
def get_locations(self):
381-
"""Returns and logs URLs of all known browser windows."""
382-
urls = [info.url for info in self._window_manager.get_window_infos()]
383-
return self._log_list(urls)
384-
385240
@keyword
386241
def get_source(self):
387242
"""Returns the entire HTML source of the current page or frame."""
@@ -392,6 +247,12 @@ def get_title(self):
392247
"""Returns the title of current page."""
393248
return self.browser.title
394249

250+
@keyword
251+
def get_location(self):
252+
"""Returns the current browser URL."""
253+
return self.browser.current_url
254+
255+
395256
@keyword
396257
def location_should_be(self, url):
397258
"""Verifies that current URL is exactly ``url``."""
@@ -696,16 +557,6 @@ def execute(self, driver_command, params=None):
696557
browser.execute = types.MethodType(execute, browser)
697558
browser._speed = self.ctx.speed
698559

699-
def _log_list(self, items, what='item'):
700-
msg = [
701-
'Altogether {} {}.'.format(
702-
len(items), what if len(items) == 1 else '{}s'.format(what))
703-
]
704-
for index, item in enumerate(items):
705-
msg.append('{}: {}'.format(index + 1, item))
706-
self.info('\n'.join(msg))
707-
return items
708-
709560
@property
710561
def _geckodriver_log_config(self):
711562
if SELENIUM_VERSION.major == '3':

0 commit comments

Comments
 (0)