Skip to content

Commit f43b649

Browse files
authored
Merge pull request #2399 from DocSpring/scroll_behavior_auto
Add "scroll-behavior: auto" CSS to Capybara::Server::AnimationDisabler
2 parents 1e8c6c2 + 7be5795 commit f43b649

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/capybara/server/animation_disabler.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ def insert_disable(html)
4646
DISABLE_MARKUP_TEMPLATE = <<~HTML
4747
<script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
4848
<style>
49-
%<selector>s, %<selector>s::before, %<selector>s::after {
49+
%<selector>s, %<selector>s::before, %<selector>s::after {
5050
transition: none !important;
5151
animation-duration: 0s !important;
5252
animation-delay: 0s !important;
53+
scroll-behavior: auto !important;
5354
}
5455
</style>
5556
HTML

lib/capybara/spec/views/with_animation.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
});
1919
</script>
2020
<style>
21+
html {
22+
scroll-behavior: smooth;
23+
}
24+
25+
body {
26+
min-height: 2000px;
27+
}
28+
2129
.transition.away {
2230
width: 0%;
2331
}

spec/shared_selenium_session.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,39 @@
388388
@animation_session.find_link('animate me away').right_click
389389
expect(@animation_session).to have_content('Animation Ended', wait: 0.1)
390390
end
391+
392+
it 'should scroll the page instantly', requires: [:js] do
393+
@animation_session.visit('with_animation')
394+
scroll_y = @animation_session.evaluate_script(<<~JS)
395+
(function(){
396+
window.scrollTo(0,500);
397+
return window.scrollY;
398+
})()
399+
JS
400+
expect(scroll_y).to eq 500
401+
end
402+
end
403+
404+
context 'when set to `false`' do
405+
before(:context) do # rubocop:disable RSpec/BeforeAfterAll
406+
skip "Safari doesn't support multiple sessions" if safari?(session)
407+
# NOTE: Although Capybara.SpecHelper.reset! sets Capybara.disable_animation to false,
408+
# it doesn't affect any of these tests because the settings are applied per-session
409+
Capybara.disable_animation = false
410+
@animation_session = Capybara::Session.new(session.mode, TestApp.new)
411+
end
412+
413+
it 'should scroll the page with a smooth animation', requires: [:js] do
414+
@animation_session.visit('with_animation')
415+
scroll_y = @animation_session.evaluate_script(<<~JS)
416+
(function(){
417+
window.scrollTo(0,500);
418+
return window.scrollY;
419+
})()
420+
JS
421+
# measured over 0.5 seconds: 0, 75, 282, 478, 500
422+
expect(scroll_y).to be < 500
423+
end
391424
end
392425

393426
context 'if we pass in css that matches elements' do

0 commit comments

Comments
 (0)