Skip to content

Commit 5206003

Browse files
ciccioman3aaltat
authored andcommitted
Get and Set Inner Window Size (#1314)
Fixes #1363
1 parent 6e1cf24 commit 5206003

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

atest/acceptance/windows.robot

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ Set Window Size using strings
7575
Should Be Equal ${width} ${600}
7676
Should Be Equal ${height} ${800}
7777

78+
Get and Set Inner Window Size
79+
Set Window Size ${800} ${600} ${True}
80+
${width} ${height}= Get Window Size ${True}
81+
Should Be Equal ${width} ${800}
82+
Should Be Equal ${height} ${600}
83+
84+
Set Inner Window Size using strings
85+
Set Window Size 800 600 ${True}
86+
${width} ${height}= Get Window Size ${True}
87+
Should Be Equal ${width} ${800}
88+
Should Be Equal ${height} ${600}
89+
90+
Get and Set Inner Window Size with Frames
91+
Go To Page "frames/frameset.html"
92+
Select Frame left
93+
Run Keyword And Expect Error
94+
... Keyword failed setting correct window size.
95+
... Set Window Size ${400} ${300} ${True}
96+
7897
Get and Set Window Position
7998
[Tags] Known Issue Chrome Known Issue Safari
8099
Set Window Position ${300} ${200}

src/SeleniumLibrary/keywords/window.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
import time
1717

18-
from SeleniumLibrary.utils import is_falsy, timestr_to_secs
18+
from SeleniumLibrary.utils import is_truthy, is_falsy, timestr_to_secs
1919
from selenium.common.exceptions import NoSuchWindowException
2020

2121
from SeleniumLibrary.base import keyword, LibraryComponent
@@ -156,19 +156,29 @@ def maximize_browser_window(self):
156156
self.driver.maximize_window()
157157

158158
@keyword
159-
def get_window_size(self):
159+
def get_window_size(self, inner=False):
160160
"""Returns current window width and height as integers.
161161
162162
See also `Set Window Size`.
163163
164+
If ``inner`` parameter is set to True, keyword returns
165+
HTML DOM window.innerWidth and window.innerHeight properties.
166+
See `Boolean arguments` for more details how to set boolean
167+
arguments. The ``inner`` is new in SeleniumLibrary 4.0.
168+
164169
Example:
165-
| ${width} | ${height}= | `Get Window Size` |
170+
| ${width} | ${height}= | `Get Window Size` | |
171+
| ${width} | ${height}= | `Get Window Size` | True |
166172
"""
173+
if is_truthy(inner):
174+
inner_width = int(self.driver.execute_script("return window.innerWidth;"))
175+
inner_height = int(self.driver.execute_script("return window.innerHeight;"))
176+
return inner_width, inner_height
167177
size = self.driver.get_window_size()
168178
return size['width'], size['height']
169179

170180
@keyword
171-
def set_window_size(self, width, height):
181+
def set_window_size(self, width, height, inner=False):
172182
"""Sets current windows size to given ``width`` and ``height``.
173183
174184
Values can be given using strings containing numbers or by using
@@ -178,10 +188,36 @@ def set_window_size(self, width, height):
178188
smaller will cause the actual size to be bigger than the requested
179189
size.
180190
191+
If ``inner`` parameter is set to True, keyword sets the necessary
192+
window width and height to have the desired HTML DOM window.innerWidth
193+
and window.innerHeight The ``inner`` is new in SeleniumLibrary 4.0.
194+
See `Boolean arguments` for more details how to set boolean
195+
arguments.
196+
197+
This ``inner`` argument does not support Frames. If a frame is selected,
198+
switch to default before running this.
199+
181200
Example:
182-
| `Set Window Size` | 800 | 600 |
201+
| `Set Window Size` | 800 | 600 | |
202+
| `Set Window Size` | 800 | 600 | True |
183203
"""
184-
return self.driver.set_window_size(int(width), int(height))
204+
width, height = int(width), int(height)
205+
if is_falsy(inner):
206+
return self.driver.set_window_size(width, height)
207+
self.driver.set_window_size(width, height)
208+
inner_width = int(self.driver.execute_script("return window.innerWidth;"))
209+
inner_height = int(self.driver.execute_script("return window.innerHeight;"))
210+
self.info('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.info('Setting window size to %s %s' % (window_width, window_height))
216+
self.driver.set_window_size(window_width, window_height)
217+
result_width = int(self.driver.execute_script("return window.innerWidth;"))
218+
result_height = int(self.driver.execute_script("return window.innerHeight;"))
219+
if result_width != width or result_height != height:
220+
raise AssertionError("Keyword failed setting correct window size.")
185221

186222
@keyword
187223
def get_window_position(self):

0 commit comments

Comments
 (0)