You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
switch_to_window does not send Chrome's Target.activateTarget CDP command #321
We have a patch in our code that speeds up coming back to a window. See the code below.
When a spec switches away from a window and later switches back, Chrome's renderer for that window remains in a backgrounded state. CDP input commands (e.g. Input.dispatchMouseEvent) then stall for ~5 seconds while Chrome reactivates the renderer. This affects any spec that uses multiple windows.
Calling page.activate after switching sends Target.activateTarget, which wakes the renderer immediately. This reduces wait time, on some specs more than 10s.
We use the following patch.
module CupriteActivateOnSwitch
# Sends Target.activateTarget after switching, waking Chrome's renderer for the target window.
# @return [true]
def switch_to_window(target_id)
super
@page.activate
end
end
Capybara::Cuprite::Browser.class_eval {prepend CupriteActivateOnSwitch}
Thanks! I'll have a look at it. I cannot share the example that we have, so I'll have to cook something else. It might takes some time because of holidays.
Capybara starting Puma...
* Version 8.0.2, codename: Into the Arena
* Min threads: 0, max threads: 4
* Listening on http://127.0.0.1:33737
unpatched: 5.16s
.patched: 0.17s
.
Finished in 8.48 seconds (files took 0.84078 seconds to load)
2 examples, 0 failures
I'm not allowed to upload a Ruby file. Save the following ruby code in a file called cuprite_activate_spec.rb (if you want it to be inline with the explanation above). You need to be in an environment that has rspec, capybara, cuprite (potentially some more that I already have in my enironment).
#
# Run as bundle exec rspec -O /dev/null <path>/cuprite_activate_spec.rb
#
# Scenario: open a second window, switch back to the first, then click a button.
# Chrome keeps the renderer of the first window backgrounded because nothing told
# it that window became the active target again. The next CDP input command
# (Input.dispatchMouseEvent) therefore stalls until Chrome reactivates the
# renderer. Sending Target.activateTarget (Ferrum's Page#activate) right after
# switching removes the stall.
require 'capybara/rspec'
require 'capybara/cuprite'
# Toggled per example so both behaviours can be shown in one run.
ACTIVATE_ON_SWITCH = {enabled: false}
module CupriteActivateOnSwitch
def switch_to_window(target_id)
super
@page.activate if ACTIVATE_ON_SWITCH[:enabled]
end
end
Capybara::Cuprite::Browser.class_eval {prepend CupriteActivateOnSwitch}
APP = lambda do |env|
body =
case env['PATH_INFO']
when '/second'
'<html><body><h1>second window</h1></body></html>'
else
<<~HTML
<html><body>
<a id="open" href="/second" target="_blank">open</a>
<button id="button" onclick="this.textContent = 'clicked'">click me</button>
</body></html>
HTML
end
[200, {'Content-Type' => 'text/html'}, [body]]
end
Capybara.register_driver(:demo_cuprite) do |app|
Capybara::Cuprite::Driver.new(app, process_timeout: 30, timeout: 30)
end
Capybara.app = APP
Capybara.default_driver = :demo_cuprite
Capybara.javascript_driver = :demo_cuprite
RSpec.describe 'clicking after switching back to a window', type: :feature do
# @return [Float] Seconds spent on the click in the original window after a
# round trip to a second window.
def seconds_for_click_after_window_round_trip
visit '/'
original_window = current_window
second_window = window_opened_by {find('#open').click}
switch_to_window(second_window)
expect(page).to have_css('h1', text: 'second window')
switch_to_window(original_window)
started_at = Time.now
find('#button').click
elapsed = Time.now - started_at
expect(page).to have_css('#button', text: 'clicked')
second_window.close
elapsed
end
context 'without the CupriteActivateOnSwitch patch' do
before {ACTIVATE_ON_SWITCH[:enabled] = false}
it 'stalls for several seconds while Chrome reactivates the renderer' do
elapsed = seconds_for_click_after_window_round_trip
puts format('unpatched: %.2fs', elapsed)
expect(elapsed).to be > 1.0
end
end
context 'with the CupriteActivateOnSwitch patch' do
before {ACTIVATE_ON_SWITCH[:enabled] = true}
it 'clicks immediately' do
elapsed = seconds_for_click_after_window_round_trip
puts format('patched: %.2fs', elapsed)
expect(elapsed).to be < 1.0
end
end
end
Activity
route commentedon Aug 16, 2026
thats interesting! do you think you can come up with a test to harden this behavior?
vdbijl commentedon Aug 16, 2026
Thanks! I'll have a look at it. I cannot share the example that we have, so I'll have to cook something else. It might takes some time because of holidays.
vdbijl commentedon Aug 17, 2026
My colleague has cooked up the following to showcase the effect. Download the script and run it via
It should output something like the following
vdbijl commentedon Aug 17, 2026
I'm not allowed to upload a Ruby file. Save the following ruby code in a file called cuprite_activate_spec.rb (if you want it to be inline with the explanation above). You need to be in an environment that has rspec, capybara, cuprite (potentially some more that I already have in my enironment).
vdbijl commentedon Aug 18, 2026
Does this work for you?
route commentedon Aug 24, 2026
Oh yea, just like you said. Fix is trivial, pushing it.
route commentedon Aug 24, 2026
fixed by 8bc6442