mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Capybara.disable_animations can also accept a CSS selector
If it receives a falsey (false or nil) it won't do anything. If it receives true, it will disable animations on the '*' css selector If it receives a string, it will use that string as the css selector It will still always disable css for jQuery if it's around.
This commit is contained in:
parent
7c5954644a
commit
443617a995
3 changed files with 73 additions and 7 deletions
|
@ -3,8 +3,20 @@
|
|||
module Capybara
|
||||
class Server
|
||||
class AnimationDisabler
|
||||
def self.selector_for(css_or_bool)
|
||||
case css_or_bool
|
||||
when String
|
||||
css_or_bool
|
||||
when true
|
||||
'*'
|
||||
else
|
||||
raise CapybaraError, 'Capybara.disable_animation supports either a String (the css selector to disable) or a boolean'
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
@disable_markup = DISABLE_MARKUP_TEMPLATE % AnimationDisabler.selector_for(Capybara.disable_animation)
|
||||
end
|
||||
|
||||
def call(env)
|
||||
|
@ -20,18 +32,20 @@ module Capybara
|
|||
|
||||
private
|
||||
|
||||
attr_reader :disable_markup
|
||||
|
||||
def html_content?
|
||||
!!(@headers['Content-Type'] =~ /html/)
|
||||
end
|
||||
|
||||
def insert_disable(html)
|
||||
html.sub(%r{(</head>)}, DISABLE_MARKUP + '\\1')
|
||||
html.sub(%r{(</head>)}, disable_markup + '\\1')
|
||||
end
|
||||
|
||||
DISABLE_MARKUP = <<~HTML
|
||||
DISABLE_MARKUP_TEMPLATE = <<~HTML
|
||||
<script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
|
||||
<style>
|
||||
* {
|
||||
%s {
|
||||
transition: none !important;
|
||||
animation-duration: 0s !important;
|
||||
animation-delay: 0s !important;
|
||||
|
|
|
@ -86,9 +86,9 @@ module Capybara
|
|||
end
|
||||
|
||||
remove_method :disable_animation=
|
||||
def disable_animation=(bool)
|
||||
warn 'Capybara.disable_animation is a beta feature - it may change/disappear in a future point version' if bool
|
||||
@disable_animation = bool
|
||||
def disable_animation=(bool_or_allowlist)
|
||||
warn 'Capybara.disable_animation is a beta feature - it may change/disappear in a future point version' if bool_or_allowlist
|
||||
@disable_animation = bool_or_allowlist
|
||||
end
|
||||
|
||||
remove_method :test_id=
|
||||
|
|
|
@ -325,12 +325,14 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
|
|||
|
||||
context 'AnimationDisabler' do
|
||||
before(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
# NOTE: Although Capybara.SpecHelper.reset! sets Capybara.disable_animation to false,
|
||||
# it doesn't affect any of these tests because the settings are applied per-session
|
||||
Capybara.disable_animation = true
|
||||
@animation_session = Capybara::Session.new(session.mode, TestApp.new)
|
||||
end
|
||||
|
||||
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
Capybara.disable_animation = false
|
||||
@animation_session = nil
|
||||
end
|
||||
|
||||
it 'should disable CSS transitions' do
|
||||
|
@ -344,6 +346,56 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
|
|||
@animation_session.click_link('animate me away')
|
||||
expect(@animation_session).to have_no_link('animate me away', wait: 0.5)
|
||||
end
|
||||
|
||||
context 'if we pass in css that matches css on screen' do
|
||||
before(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
# NOTE: Although Capybara.SpecHelper.reset! sets Capybara.disable_animation to false,
|
||||
# it doesn't affect any of these tests because the settings are applied per-session
|
||||
Capybara.disable_animation = '#with_animation a'
|
||||
@animation_session_with_matching_css = Capybara::Session.new(session.mode, TestApp.new)
|
||||
end
|
||||
|
||||
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
@animation_session_with_matching_css = nil
|
||||
end
|
||||
|
||||
it 'should disable CSS transitions' do
|
||||
@animation_session_with_matching_css.visit('with_animation')
|
||||
@animation_session_with_matching_css.click_link('transition me away')
|
||||
expect(@animation_session_with_matching_css).to have_no_link('transition me away', wait: 0.5)
|
||||
end
|
||||
|
||||
it 'should disable CSS animations' do
|
||||
@animation_session_with_matching_css.visit('with_animation')
|
||||
@animation_session_with_matching_css.click_link('animate me away')
|
||||
expect(@animation_session_with_matching_css).to have_no_link('animate me away', wait: 0.5)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if we pass in css that does not matches css on screen' do
|
||||
before(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
# NOTE: Although Capybara.SpecHelper.reset! sets Capybara.disable_animation to false,
|
||||
# it doesn't affect any of these tests because the settings are applied per-session
|
||||
Capybara.disable_animation = '.this-class-matches-nothing'
|
||||
@animation_session_without_matching_css = Capybara::Session.new(session.mode, TestApp.new)
|
||||
end
|
||||
|
||||
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
|
||||
@animation_session_without_matching_css = nil
|
||||
end
|
||||
|
||||
it 'should disable CSS transitions' do
|
||||
@animation_session_without_matching_css.visit('with_animation')
|
||||
@animation_session_without_matching_css.click_link('transition me away')
|
||||
expect(@animation_session_without_matching_css).to have_link('transition me away', wait: 0.5)
|
||||
end
|
||||
|
||||
it 'should disable CSS animations' do
|
||||
@animation_session_without_matching_css.visit('with_animation')
|
||||
@animation_session_without_matching_css.click_link('animate me away')
|
||||
expect(@animation_session_without_matching_css).to have_link('animate me away', wait: 0.5)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue