Skip to content

Commit 1d8ffc7

Browse files
committed
Override the viewport size when mobile: true
This is a bit hacky; ideally, a caller wouldn't both specify a non-mobile size *and* mobile: true.
1 parent 33b16bd commit 1d8ffc7

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

lib/ferrum/page.rb

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ def close_connection
136136
end
137137

138138
#
139-
# Overrides device screen dimensions and emulates viewport according to parameters
139+
# Overrides device screen dimensions and emulates viewport according to parameters.
140+
#
141+
# Note that passing mobile: true will cause set_viewport to ignore the passed
142+
# height and width values, and instead use 390 x 844, which is the viewport size
143+
# of an iPhone 14.
140144
#
141145
# Read more [here](https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setDeviceMetricsOverride).
142146
#
@@ -149,27 +153,36 @@ def close_connection
149153
# @param [Boolean] mobile whether to emulate mobile device
150154
#
151155
def set_viewport(width:, height:, scale_factor: 0, mobile: false)
152-
command(
153-
"Emulation.setDeviceMetricsOverride",
154-
slowmoable: true,
155-
width: width,
156-
height: height,
157-
deviceScaleFactor: scale_factor,
158-
mobile: mobile
159-
)
160-
161-
options = if mobile
162-
{
163-
enabled: true,
164-
maxTouchPoints: 1
165-
}
166-
else
167-
{
168-
enabled: false
169-
}
170-
end
171-
172-
command("Emulation.setTouchEmulationEnabled", **options)
156+
if mobile
157+
command(
158+
"Emulation.setTouchEmulationEnabled",
159+
enabled: true,
160+
maxTouchPoints: 1
161+
)
162+
163+
command(
164+
"Emulation.setDeviceMetricsOverride",
165+
deviceScaleFactor: 3.0,
166+
height: 844,
167+
mobile: true,
168+
slowmoable: true,
169+
width: 390
170+
)
171+
else
172+
command(
173+
"Emulation.setTouchEmulationEnabled",
174+
enabled: false
175+
)
176+
177+
command(
178+
"Emulation.setDeviceMetricsOverride",
179+
deviceScaleFactor: scale_factor,
180+
height: height,
181+
mobile: false,
182+
slowmoable: true,
183+
width: width
184+
)
185+
end
173186
end
174187

175188
def resize(width: nil, height: nil, fullscreen: false, mobile: false)

0 commit comments

Comments
 (0)