1515# limitations under the License.
1616import time
1717
18- from SeleniumLibrary .utils import is_falsy , timestr_to_secs
18+ from SeleniumLibrary .utils import is_truthy , is_falsy , timestr_to_secs
1919from selenium .common .exceptions import NoSuchWindowException
2020
2121from 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