Skip to content

Commit 6312a9d

Browse files
committed
Add mobile support to Page
Note that when mobile is true, *both* mobile emulation and touch support are enabled. The spec checks whether mobile support has been enabled by checking for touch support with JavaScript.
1 parent 42184d6 commit 6312a9d

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/ferrum/page.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,22 @@ def set_viewport(width:, height:, scale_factor: 0, mobile: false)
157157
deviceScaleFactor: scale_factor,
158158
mobile: mobile
159159
)
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)
160173
end
161174

162-
def resize(width: nil, height: nil, fullscreen: false)
175+
def resize(width: nil, height: nil, fullscreen: false, mobile: false)
163176
if fullscreen
164177
width, height = document_size
165178
self.window_bounds = { window_state: "fullscreen" }
@@ -168,7 +181,7 @@ def resize(width: nil, height: nil, fullscreen: false)
168181
self.window_bounds = { width: width, height: height }
169182
end
170183

171-
set_viewport(width: width, height: height)
184+
set_viewport(width: width, height: height, mobile: mobile)
172185
end
173186

174187
#

spec/page_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,48 @@
235235
wait_for { message_b }.to eq("goodbye")
236236
end
237237
end
238+
239+
describe "#resize" do
240+
def body_size
241+
{
242+
height: page.evaluate("document.body.clientHeight"),
243+
width: page.evaluate("document.body.clientWidth")
244+
}
245+
end
246+
247+
def is_mobile?
248+
page.evaluate("'ontouchstart' in window || navigator.maxTouchPoints > 0")
249+
end
250+
251+
before do
252+
page.go_to("/")
253+
end
254+
255+
context "given a different size" do
256+
it "resizes the page" do
257+
expect { page.resize(width: 2000, height: 1000) }.to change { body_size }.to(width: 2000, height: 1000)
258+
end
259+
end
260+
261+
context "given a zero height" do
262+
it "does not change the height" do
263+
expect { page.resize(width: 2000, height: 0) }.not_to change { body_size[:height] }
264+
end
265+
end
266+
267+
context "given a zero width" do
268+
it "does not change the width" do
269+
expect { page.resize(width: 0, height: 1000) }.not_to change { body_size[:width] }
270+
end
271+
end
272+
273+
context "when mobile is true" do
274+
it "enables mobile emulation in the browser" do
275+
expect do
276+
page.resize(width: 0, height: 0, mobile: true)
277+
page.reload
278+
end.to change { is_mobile? }.to(true)
279+
end
280+
end
281+
end
238282
end

0 commit comments

Comments
 (0)