Skip to content

Commit 8426118

Browse files
authored
Merge pull request #152 from rubycdp/refactor-page-resize
2 parents f0d9d70 + 3ba27d8 commit 8426118

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/ferrum/page.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,12 @@ def close
9292
end
9393

9494
def resize(width: nil, height: nil, fullscreen: false)
95-
result = @browser.command("Browser.getWindowForTarget", targetId: @target_id)
96-
@window_id, @bounds = result.values_at("windowId", "bounds")
97-
9895
if fullscreen
9996
width, height = document_size
100-
@browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "fullscreen" })
97+
set_window_bounds(windowState: "fullscreen")
10198
else
102-
@browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "normal" })
103-
@browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { width: width, height: height, windowState: "normal" })
99+
set_window_bounds(windowState: "normal")
100+
set_window_bounds(width: width, height: height)
104101
end
105102

106103
command("Emulation.setDeviceMetricsOverride", slowmoable: true,
@@ -140,6 +137,14 @@ def bypass_csp(value = true)
140137
enabled
141138
end
142139

140+
def window_id
141+
@browser.command("Browser.getWindowForTarget", targetId: @target_id)["windowId"]
142+
end
143+
144+
def set_window_bounds(bounds = {})
145+
@browser.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
146+
end
147+
143148
def command(method, wait: 0, slowmoable: false, **params)
144149
iteration = @event.reset if wait > 0
145150
sleep(@browser.slowmo) if slowmoable && @browser.slowmo > 0

spec/browser_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ module Ferrum
116116
let(:path) { "/ferrum/custom_html_size_100%" }
117117
let(:viewport_size) { [1272, 1008] }
118118
end
119+
120+
it "resizes to 'normal' from 'fullscreen' window state" do
121+
browser.go_to(path)
122+
browser.resize(fullscreen: true)
123+
browser.resize(width: 200, height: 400)
124+
expect(browser.viewport_size).to eq([200, 400])
125+
end
119126
end
120127

121128
it "allows the page to be scrolled" do

0 commit comments

Comments
 (0)