Skip to content

feat(browser): add on_page to set up every page before Ferrum hands it out - #735

Merged
route merged 1 commit into
mainfrom
on-page-hook
Nov 26, 2026
Merged

route merged 1 commit into
mainfrom
on-page-hook

Conversation

@route

@route route commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

There's no public way to set up a page before anything happens on it. Code that wants to handle a popup's dialogs, or configure every page the same way, has to subclass Page and build pages through private API (Page.new, target.page=, maybe_sleep_if_new_window). Cuprite does exactly that today.

  • browser.on_page { |page| } registers a block that Target#build_page calls with each page it builds, before returning it. It covers pages from create_page, the default page, and pages another page opens. Pages already open aren't passed to it.
  • It's not a Browser.new option. Like add_extension, the browser delegates it to its options, which store the blocks. Each call adds a block, so a library and the user's own code can each register one.
  • on_page without a block raises ArgumentError.
  • No new links between objects: Target already holds the browser options.
  • Docs (docs/3-navigation.md), RBS and the changelog are updated.
  • New specs: a page from create_page is passed to the block before the call returns, a popup is passed too, and a call without a block raises.

This is the first step of moving Cuprite off its Ferrum::Page subclass. Cuprite will register its dialog handling, JS errors and viewport setup with on_page right after Browser.new, before any page exists, and keep its own per-page state next to the page.

@route route changed the title feat(browser): add :on_page to set up every page before Ferrum hands it out feat(browser): add on_page to set up every page before Ferrum hands it out Nov 26, 2026
…t out

browser.on_page { |page| } registers a block, stored on the options like add_extension, that Target#build_page calls with each page it
builds, created or opened by another page, before returning it. Callers can subscribe to a page's events or configure it before anything
happens on it, without subclassing Page.
@route
route merged commit 6c7b978 into main Nov 26, 2026
7 checks passed
@route
route deleted the on-page-hook branch November 26, 2026 18:25
route added a commit to rubycdp/cuprite that referenced this pull request Nov 26, 2026
route added a commit to rubycdp/cuprite that referenced this pull request Nov 26, 2026
… in the driver (#399)

* chore(deps): build against Ferrum's on-page-hook branch until rubycdp/ferrum#735 is merged

* ref(browser): set pages up through Ferrum's on_page instead of subclassing Ferrum::Page

Cuprite::Page overrode Ferrum::Page's private subscribe and prepare_page, and attach_page built it through Target's private
maybe_sleep_if_new_window and page=. Browser now registers one on_page block that sets every page up with Ferrum's public API,
and keeps the frame stack and the dialog queue in a PageState per target.

- Browser delegates the DOM and JavaScript methods to the frame switched to, and overrides go_to and set_viewport for the
  pending connections error and the scale_factor: and mobile: defaults
- resize applies those defaults again, since Ferrum's Page#resize sets the viewport without them
- An unhandled dialog's error is raised around the Driver and Node calls instead of around every page command

* chore(deps): build against Ferrum main again now that rubycdp/ferrum#735 and #736 are merged

* ref(driver): drive a plain Ferrum browser and keep Capybara's windows in the driver

Cuprite::Browser subclassed Ferrum::Browser to keep the current window, the per-page frames and dialogs, the settings and
the page setup. The driver keeps them now, so driver.browser is a plain Ferrum::Browser whose page is the first window.

- Driver#current_page is the window Capybara switched to, and Driver#window(page) holds its frame stack and dialog queue
- The window handle methods move to Driver::Windows, the settings to Driver::Settings
- url_blocklist=, url_allowlist=, their aliases and raise_on_unhandled_modal= move from driver.browser to driver
- Driver#reset! restarts a browser that died, Browser#command did it before
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.

1 participant