Robot Framework SeleniumLibrary
window.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import time
17 
18 from SeleniumLibrary.utils import is_truthy, is_falsy, timestr_to_secs
19 from selenium.common.exceptions import NoSuchWindowException
20 
21 from SeleniumLibrary.base import keyword, LibraryComponent
22 from SeleniumLibrary.locators import WindowManager
23 from SeleniumLibrary.utils import plural_or_not
24 
25 
27 
28  def __init__(self, ctx):
29  LibraryComponent.__init__(self, ctx)
30  self._window_manager_window_manager = WindowManager(ctx)
31 
32  @keyword
33 
104  def select_window(self, locator='MAIN', timeout=None):
105  epoch = time.time()
106  timeout = epoch if is_falsy(timeout) else timestr_to_secs(timeout) + epoch
107  try:
108  return self.driverdriverdriver.current_window_handle
109  except NoSuchWindowException:
110  pass
111  finally:
112  self._window_manager_window_manager.select(locator, timeout)
113 
114  @keyword
115 
116  def close_window(self):
117  self.driverdriverdriver.close()
118 
119  @keyword
120 
127  return self.driverdriverdriver.window_handles
128 
129  @keyword
130 
132  ids = [info.id for info in self._window_manager_window_manager.get_window_infos()]
133  return self._log_list_log_list(ids)
134 
135  @keyword
136 
137  def get_window_names(self):
138  names = [info.name for info in self._window_manager_window_manager.get_window_infos()]
139  return self._log_list_log_list(names)
140 
141  @keyword
142 
143  def get_window_titles(self):
144  titles = [info.title for info in self._window_manager_window_manager.get_window_infos()]
145  return self._log_list_log_list(titles)
146 
147  @keyword
148 
149  def get_locations(self):
150  urls = [info.url for info in self._window_manager_window_manager.get_window_infos()]
151  return self._log_list_log_list(urls)
152 
153  @keyword
154 
156  self.driverdriverdriver.maximize_window()
157 
158  @keyword
159 
172  def get_window_size(self, inner=False):
173  if is_truthy(inner):
174  inner_width = int(self.driverdriverdriver.execute_script("return window.innerWidth;"))
175  inner_height = int(self.driverdriverdriver.execute_script("return window.innerHeight;"))
176  return inner_width, inner_height
177  size = self.driverdriverdriver.get_window_size()
178  return size['width'], size['height']
179 
180  @keyword
181 
203  def set_window_size(self, width, height, inner=False):
204  width, height = int(width), int(height)
205  if is_falsy(inner):
206  return self.driverdriverdriver.set_window_size(width, height)
207  self.driverdriverdriver.set_window_size(width, height)
208  inner_width = int(self.driverdriverdriver.execute_script("return window.innerWidth;"))
209  inner_height = int(self.driverdriverdriver.execute_script("return window.innerHeight;"))
210  self.infoinfo('window.innerWidth is %s and window.innerHeight is %s' % (inner_width, inner_height))
211  width_offset = width - inner_width
212  height_offset = height - inner_height
213  window_width = width + width_offset
214  window_height = height + height_offset
215  self.infoinfo('Setting window size to %s %s' % (window_width, window_height))
216  self.driverdriverdriver.set_window_size(window_width, window_height)
217  result_width = int(self.driverdriverdriver.execute_script("return window.innerWidth;"))
218  result_height = int(self.driverdriverdriver.execute_script("return window.innerHeight;"))
219  if result_width != width or result_height != height:
220  raise AssertionError("Keyword failed setting correct window size.")
221 
222  @keyword
223 
232  position = self.driverdriverdriver.get_window_position()
233  return position['x'], position['y']
234 
235  @keyword
236 
249  def set_window_position(self, x, y):
250  self.driverdriverdriver.set_window_position(int(x), int(y))
251 
252  def _log_list(self, items, what='item'):
253  msg = [
254  'Altogether %s %s%s.'
255  % (len(items), what, plural_or_not(items))
256  ]
257  for index, item in enumerate(items):
258  msg.append('%s: %s' % (index + 1, item))
259  self.infoinfo('\n'.join(msg))
260  return items
def select_window(self, locator='MAIN', timeout=None)
Selects browser window matching locator.
Definition: window.py:104
def set_window_size(self, width, height, inner=False)
Sets current windows size to given width and height.
Definition: window.py:203
def close_window(self)
Closes currently opened pop-up window.
Definition: window.py:116
def maximize_browser_window(self)
Maximizes current browser window.
Definition: window.py:155
def get_window_names(self)
Returns and logs names of all known browser windows.
Definition: window.py:137
def get_window_handles(self)
Return all current window handles as a list.
Definition: window.py:126
def get_window_identifiers(self)
Returns and logs id attributes of all known browser windows.
Definition: window.py:131
def __init__(self, ctx)
Base class exposing attributes from the common context.
Definition: window.py:28
def get_window_position(self)
Returns current window position.
Definition: window.py:231
def get_window_titles(self)
Returns and logs titles of all known browser windows.
Definition: window.py:143
def set_window_position(self, x, y)
Sets window position using x and y coordinates.
Definition: window.py:249
def get_window_size(self, inner=False)
Returns current window width and height as integers.
Definition: window.py:172
def _log_list(self, items, what='item')
Definition: window.py:252
def get_locations(self)
Returns and logs URLs of all known browser windows.
Definition: window.py:149