1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Merge pull request #2085 from michaelglass/allow-exceptions-to-animation-disabler

animation disabler can accept CSS as well as boolean
This commit is contained in:
Thomas Walpole 2018-08-21 08:54:46 -07:00 committed by GitHub
commit 61563029ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 7 deletions

View file

@ -3,8 +3,20 @@
module Capybara module Capybara
class Server class Server
class AnimationDisabler 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) def initialize(app)
@app = app @app = app
@disable_markup = DISABLE_MARKUP_TEMPLATE % AnimationDisabler.selector_for(Capybara.disable_animation)
end end
def call(env) def call(env)
@ -20,18 +32,20 @@ module Capybara
private private
attr_reader :disable_markup
def html_content? def html_content?
!!(@headers['Content-Type'] =~ /html/) !!(@headers['Content-Type'] =~ /html/)
end end
def insert_disable(html) def insert_disable(html)
html.sub(%r{(</head>)}, DISABLE_MARKUP + '\\1') html.sub(%r{(</head>)}, disable_markup + '\\1')
end end
DISABLE_MARKUP = <<~HTML DISABLE_MARKUP_TEMPLATE = <<~HTML
<script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script> <script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
<style> <style>
* { %s {
transition: none !important; transition: none !important;
animation-duration: 0s !important; animation-duration: 0s !important;
animation-delay: 0s !important; animation-delay: 0s !important;

View file

@ -86,9 +86,9 @@ module Capybara
end end
remove_method :disable_animation= remove_method :disable_animation=
def 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 warn 'Capybara.disable_animation is a beta feature - it may change/disappear in a future point version' if bool_or_allowlist
@disable_animation = bool @disable_animation = bool_or_allowlist
end end
remove_method :test_id= remove_method :test_id=

View file

@ -325,12 +325,14 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
context 'AnimationDisabler' do context 'AnimationDisabler' do
before(:context) do # rubocop:disable RSpec/BeforeAfterAll 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 Capybara.disable_animation = true
@animation_session = Capybara::Session.new(session.mode, TestApp.new) @animation_session = Capybara::Session.new(session.mode, TestApp.new)
end end
after(:context) do # rubocop:disable RSpec/BeforeAfterAll after(:context) do # rubocop:disable RSpec/BeforeAfterAll
Capybara.disable_animation = false @animation_session = nil
end end
it 'should disable CSS transitions' do 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') @animation_session.click_link('animate me away')
expect(@animation_session).to have_no_link('animate me away', wait: 0.5) expect(@animation_session).to have_no_link('animate me away', wait: 0.5)
end 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
end end