Skip to content

Enable mobile emulation - #292

Open
duncan-bayne wants to merge 7 commits into
rubycdp:mainfrom
radiopaedia:216-mobile-emulation
Open

Enable mobile emulation#292
duncan-bayne wants to merge 7 commits into
rubycdp:mainfrom
radiopaedia:216-mobile-emulation

Conversation

@duncan-bayne

@duncan-bayne duncan-bayne commented Mar 21, 2025

Copy link
Copy Markdown
Contributor

🚨 Needs rubycdp/ferrum#527 before merging. 🚨

Currently, when creating a Cuprite driver, it's not possible to specify that it should have mobile emulation enabled.

This PR adds that functionality.

Remember to revert 5117a90 once rubycdp/ferrum#527 is merged.

@duncan-bayne
duncan-bayne force-pushed the 216-mobile-emulation branch 3 times, most recently from d2de7e2 to 410be86 Compare March 27, 2025 12:26
@duncan-bayne
duncan-bayne force-pushed the 216-mobile-emulation branch 2 times, most recently from cc67c32 to d2405cd Compare May 14, 2025 01:57
@duncan-bayne
duncan-bayne force-pushed the 216-mobile-emulation branch from d2405cd to 7312657 Compare May 4, 2026 05:42
We need to expose the mobile setting in the Ferrum API as well. Until
that's merged, use a fork of Ferrum that exposes it.
This tests for mobile emulation by:

* Checking that text controlled by a CSS media query is / isn't
visible.

* Checking that touch input is enabled / disabled.
These were (a) trying to use a non-existent session, and (b) trying to
set both `mobile: true` and a resolution, which is now disallowed.
@duncan-bayne
duncan-bayne force-pushed the 216-mobile-emulation branch from cbcfb69 to b4e8fef Compare May 4, 2026 05:57
@duncan-bayne
duncan-bayne marked this pull request as ready for review May 13, 2026 01:42
@route

route commented Aug 24, 2026

Copy link
Copy Markdown
Member

Notes from reviewing the Ferrum side of this (rubycdp/ferrum#527):

What "mobile emulation" actually is

It's purely a viewport-level concern, made of two independent CDP calls:

  1. Emulation.setDeviceMetricsOverride(mobile: true, ...) — makes the page honor <meta name="viewport"> the way a real mobile browser does.
  2. Emulation.setTouchEmulationEnabled(enabled: true, ...) — touch events / navigator.maxTouchPoints.

Neither of those needs a specific device size or a mobile User-Agent to work — those are separate, optional pieces (which is exactly what Devices::IPHONE_14 in this PR bundles on the Cuprite side).

What changed on the Ferrum side (and why)

Ferrum::Page#set_viewport(mobile:) already did (1); rubycdp/ferrum#527 originally added (2) to set_viewport too, which is correct — but it also threaded mobile: through Page#resize, which is not correct: resize is Capybara's generic window-resize hook (resize_window_to), unrelated to device emulation. Coupling the two caused a real bug: resize(width: 0, height: 0, mobile: true) forwarded the 0s straight into Browser.setWindowBounds, which (unlike the CDP viewport override) doesn't treat 0 as "leave this alone" — so enabling mobile emulation without a size preset was silently shrinking the actual browser window.

Rather than patching that bug in resize, I removed mobile: from resize entirely and left set_viewport as the sole mobile/touch entry point: https://github.com/rubycdp/ferrum/tree/mobile-emulation-527.
(#527 itself is stale/CONFLICTING against current main)

What this means for this PR

lib/capybara/cuprite/page.rb currently does:

width, height = @options.window_size
resize(width: width, height: height, mobile: @options.mobile)

This will break against the updated Ferrum API (resize no longer accepts mobile:). It needs to become something like:

width, height = @options.window_size
resize(width: width, height: height)
set_viewport(width: width, height: height, mobile: @options.mobile) if @options.mobile

Everything else in this PR (Devices::IPHONE_14, the mobile spec page/driver registration) looks right as-is and doesn't need to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants