Skip to content

Instantly share code, notes, and snippets.

@keithtom
Last active September 22, 2019 06:49
Show Gist options
  • Save keithtom/8763169 to your computer and use it in GitHub Desktop.
Save keithtom/8763169 to your computer and use it in GitHub Desktop.
Rack Middleware to disable Disable CSS3/jQuery Animations for Capybara
# Copyright (C) [2014] by Keith Tom <keith dot tom at gmail>
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
module Rack
# disable CSS3 and jQuery animations in test mode for speed, consistency and avoiding timing issues.
# Usage for Rails:
# in config/environments/test.rb
# config.middleware.use Rack::NoAnimations
class NoAnimations
def initialize(app, options = {})
@app = app
end
def call(env)
@status, @headers, @body = @app.call(env)
return [@status, @headers, @body] unless html?
response = Rack::Response.new([], @status, @headers)
@body.each { |fragment| response.write inject(fragment) }
@body.close if @body.respond_to?(:close)
response.finish
end
private
def html?
@headers["Content-Type"] =~ /html/
end
def inject(fragment)
disable_animations = <<-EOF
<script type="text/javascript">(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
<style>
* {
-o-transition: none !important;
-moz-transition: none !important;
-ms-transition: none !important;
-webkit-transition: none !important;
transition: none !important;
-o-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
-webkit-transform: none !important;
transform: none !important;
-webkit-animation: none !important;
-moz-animation: none !important;
-o-animation: none !important;
-ms-animation: none !important;
animation: none !important;
}
</style>
EOF
fragment.gsub(%r{</head>}, disable_animations + "</head>")
end
end
end
@sidazhou
Copy link

cool, it worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment