Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Poltergeist hack to silence CoreText performance notes from phantomjs
module Capybara::Poltergeist
class Client
private
def redirect_stdout(to)
prev = STDOUT.dup
prev.autoclose = false
$stdout = to
STDOUT.reopen(to)
prev = STDERR.dup
prev.autoclose = false
$stderr = to
STDERR.reopen(to)
yield
ensure
STDOUT.reopen(prev)
$stdout = STDOUT
STDERR.reopen(prev)
$stderr = STDERR
end
end
end
class WarningSuppressor
class << self
def write(message)
if message =~ /QFont::setPixelSize: Pixel size <= 0/ || message =~/CoreText performance note:/ then 0 else puts(message);1;end
end
end
end
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, phantomjs_logger: WarningSuppressor)
end
Capybara.javascript_driver = :poltergeist
@johnpaulashenfelter

This comment has been minimized.

Copy link

johnpaulashenfelter commented Oct 23, 2013

wrong number of arguments (0 for 1)
# ./spec/support/monkeypatch_mavericks.rb:4:in `redirect_stdout'

any ideas? ruby 2.0.0-p247, rails 3.2.15

I'll rtfm in the meantime :)

@matheusbras

This comment has been minimized.

Copy link

matheusbras commented Oct 24, 2013

Same thing here. ruby 2.0.0-p195, rails 4.0.0

@matheusbras

This comment has been minimized.

Copy link

matheusbras commented Oct 24, 2013

This worked for me

def redirect_stdout
  prev = STDOUT.dup
  prev.autoclose = false
  $stdout = @write_io
  STDOUT.reopen(@write_io)

  prev = STDERR.dup
  prev.autoclose = false
  $stderr = @write_io
  STDERR.reopen(@write_io)
  yield
ensure
  STDOUT.reopen(prev)
  $stdout = STDOUT
  STDERR.reopen(prev)
  $stderr = STDERR
end
@pbrit

This comment has been minimized.

Copy link

pbrit commented Oct 30, 2013

Thanks! It works!

@EdgarOrtegaRamirez

This comment has been minimized.

Copy link

EdgarOrtegaRamirez commented Oct 30, 2013

Thanks @matheusbras 👍

@robwierzbowski

This comment has been minimized.

Copy link

robwierzbowski commented Oct 30, 2013

Thanks @matheusbras.

Can anyone give little help on what I'd need to change to apply this to the :poltergeist_billy driver? Not a ruby developer but using Cappy in our test suite.

@hadees

This comment has been minimized.

@lvonk

This comment has been minimized.

Copy link

lvonk commented Nov 13, 2013

Thanks for sharing this gist! Works for me.

@Irostovsky

This comment has been minimized.

Copy link

Irostovsky commented Nov 15, 2013

Thanks!

@clupprich

This comment has been minimized.

Copy link

clupprich commented Nov 15, 2013

@ericboehs Now a really dumb question: Isn't prevoverwriting each other?

@jaredbeck

This comment has been minimized.

Copy link

jaredbeck commented Dec 6, 2013

Extending this pattern for other unwanted output:

class WarningSuppressor
  IGNORES = [
    /QFont::setPixelSize: Pixel size <= 0/,
    /CoreText performance note:/,
    /Heya! This page is using wysihtml5/
  ]

  class << self
    def write(message)
      if suppress?(message) then 0 else puts(message);1;end
    end

    private

    def suppress?(message)
      IGNORES.any? { |re| message =~ re }
    end
  end
end
@ErinCall

This comment has been minimized.

Copy link

ErinCall commented Feb 13, 2014

I had trouble making this cooperate with a phantomjs installed by phantomjs-gem. I straightened it out by changing the way the Driver's initialized:

require 'phantomjs'

#...everything as described above...

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, phantomjs_logger: WarningSuppressor, phantomjs: Phantomjs.path)
end

Just noting this for anyone who comes along with the same problem :~)

@uiur

This comment has been minimized.

Copy link

uiur commented Apr 30, 2014

I created a gem: https://github.com/uiureo/poltergeist-suppressor

You can write like this:

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app,
    phantomjs_logger: Capybara::Poltergeist::Suppressor.new
  )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.