@@ -156,10 +156,10 @@ def set_viewport(width:, height:, scale_factor: 0, mobile: false)
156156 def resize ( width : nil , height : nil , fullscreen : false )
157157 if fullscreen
158158 width , height = document_size
159- set_window_bounds ( windowState : "fullscreen" )
159+ self . window_bounds = { window_state : "fullscreen" }
160160 else
161- set_window_bounds ( windowState : "normal" )
162- set_window_bounds ( width : width , height : height )
161+ self . window_bounds = { window_state : "normal" }
162+ self . window_bounds = { width : width , height : height }
163163 end
164164
165165 set_viewport ( width : width , height : height )
@@ -184,9 +184,7 @@ def disable_javascript
184184 # page.position # => [10, 20]
185185 #
186186 def position
187- client ( browser : true )
188- . command ( "Browser.getWindowBounds" , windowId : window_id )
189- . fetch ( "bounds" ) . values_at ( "left" , "top" )
187+ window_bounds . values_at ( "left" , "top" )
190188 end
191189
192190 #
@@ -204,9 +202,61 @@ def position
204202 # page.position = { left: 10, top: 20 }
205203 #
206204 def position = ( options )
207- client ( browser : true ) . command ( "Browser.setWindowBounds" ,
208- windowId : window_id ,
209- bounds : { left : options [ :left ] , top : options [ :top ] } )
205+ self . window_bounds = { left : options [ :left ] , top : options [ :top ] }
206+ end
207+
208+ # Sets the position of the window.
209+ #
210+ # @param [Hash{Symbol => Object}] bounds
211+ #
212+ # @option options [Integer] :left
213+ # The number of pixels from the left-hand side of the screen.
214+ #
215+ # @option options [Integer] :top
216+ # The number of pixels from the top of the screen.
217+ #
218+ # @option options [Integer] :width
219+ # The window width in pixels.
220+ #
221+ # @option options [Integer] :height
222+ # The window height in pixels.
223+ #
224+ # @option options [String] :window_state
225+ # The window state. Default to normal. Allowed Values: normal, minimized, maximized, fullscreen
226+ #
227+ # @example
228+ # page.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
229+ #
230+ def window_bounds = ( bounds )
231+ options = bounds . dup
232+ window_state = options . delete ( :window_state )
233+ bounds = { windowState : window_state , **options } . compact
234+
235+ client . command ( "Browser.setWindowBounds" , windowId : window_id , bounds : bounds )
236+ end
237+
238+ #
239+ # Current window bounds.
240+ #
241+ # @return [Hash{String => (Integer, String)}]
242+ #
243+ # @example
244+ # page.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
245+ #
246+ def window_bounds
247+ client . command ( "Browser.getWindowBounds" , windowId : window_id ) . fetch ( "bounds" )
248+ end
249+
250+ #
251+ # Current window id.
252+ #
253+ # @return [Integer]
254+ #
255+ # @example
256+ # page.window_id # => 1
257+ #
258+ def window_id
259+ client . command ( "Browser.getWindowForTarget" , targetId : target_id ) [ "windowId" ]
210260 end
211261
212262 #
@@ -282,14 +332,6 @@ def bypass_csp(enabled: true)
282332 enabled
283333 end
284334
285- def window_id
286- client ( browser : true ) . command ( "Browser.getWindowForTarget" , targetId : @target_id ) [ "windowId" ]
287- end
288-
289- def set_window_bounds ( bounds = { } )
290- client ( browser : true ) . command ( "Browser.setWindowBounds" , windowId : window_id , bounds : bounds )
291- end
292-
293335 def command ( method , wait : 0 , slowmoable : false , **params )
294336 iteration = @event . reset if wait . positive?
295337 sleep ( @options . slowmo ) if slowmoable && @options . slowmo . positive?
0 commit comments