Skip to content

Commit

Permalink
Merge pull request dbalatero#5 from dbalatero/feature/filtering-by-lo…
Browse files Browse the repository at this point in the history
…g-level

Add filtering for severity level
  • Loading branch information
dbalatero authored Jun 23, 2018
2 parents ac749dd + 7498e20 commit 54e9070
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 18 deletions.
141 changes: 141 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
AllCops:
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
# to ignore them, so only the ones explicitly set in this file are enabled.
DisabledByDefault: true
TargetRubyVersion: 2.3

# Prefer &&/|| over and/or.
Style/AndOr:
Enabled: true

# Do not use braces for hash literals when they are the last argument of a
# method call.
Style/BracesAroundHashParameters:
Enabled: true

# Align `when` with `case`.
Layout/CaseIndentation:
Enabled: true

# Align comments with method definitions.
Layout/CommentIndentation:
Enabled: true

Layout/EmptyLineAfterMagicComment:
Enabled: true

# No extra empty lines.
Layout/EmptyLines:
Enabled: true

# In a regular class definition, no empty lines around the body.
Layout/EmptyLinesAroundClassBody:
Enabled: true

# In a regular method definition, no empty lines around the body.
Layout/EmptyLinesAroundMethodBody:
Enabled: true

# In a regular module definition, no empty lines around the body.
Layout/EmptyLinesAroundModuleBody:
Enabled: true

# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
Style/HashSyntax:
Enabled: true

# Method definitions after `private` or `protected` isolated calls need one
# extra level of indentation.
Layout/IndentationConsistency:
Enabled: true
EnforcedStyle: normal

# Two spaces, no tabs (for indentation).
Layout/IndentationWidth:
Enabled: true

Layout/SpaceAfterColon:
Enabled: true

Layout/SpaceAfterComma:
Enabled: true

Layout/SpaceAroundEqualsInParameterDefault:
Enabled: true

Layout/SpaceAroundKeyword:
Enabled: true

Layout/SpaceAroundOperators:
Enabled: true

Layout/SpaceBeforeFirstArg:
Enabled: true

# Defining a method with parameters needs parentheses.
Style/MethodDefParentheses:
Enabled: true

# Use `foo {}` not `foo{}`.
Layout/SpaceBeforeBlockBraces:
Enabled: true

# Use `foo { bar }` not `foo {bar}`.
Layout/SpaceInsideBlockBraces:
Enabled: true

# Use `{ a: 1 }` not `{a:1}`.
Layout/SpaceInsideHashLiteralBraces:
Enabled: true

Layout/SpaceInsideParens:
Enabled: true

# Don't care about single vs double quotes
Style/StringLiterals:
Enabled: false
SupportedStyles:
- single_quotes
- double_quotes

# Detect hard tabs, no hard tabs.
Layout/Tab:
Enabled: true

# Blank lines should not have any spaces.
Layout/TrailingBlankLines:
Enabled: true

Metrics/LineLength:
Enabled: true
Max: 80
IgnoredPatterns: ['\A\s*class ']

Layout/MultilineMethodCallIndentation:
Enabled: true
EnforcedStyle: indented
IndentationWidth: 2

# No trailing whitespace.
Layout/TrailingWhitespace:
Enabled: true

# Use quotes for string literals when they are enough.
Style/UnneededPercentQ:
Enabled: true

# Align `end` with the matching keyword or starting expression except for
# assignments, where it should be aligned with the LHS.
Lint/EndAlignment:
Enabled: true
EnforcedStyleAlignWith: variable

# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
Lint/RequireParentheses:
Enabled: true

Style/BlockDelimiters:
Enabled: true

Style/SymbolArray:
Enabled: true
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
sudo: false
language: ruby
addons:
apt:
sources:
- google-chrome
packages:
- google-chrome-stable
rvm:
- 2.4.1
before_install: gem install bundler -v 1.16.0
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ Capybara::Chromedriver::Logger.filters = [
/Download the React DevTools/i,
/The SSL certificate used to load resources from/i
]

# If you want to filter out specific severity levels, you can do so here:
#
# default: nil
Capybara::Chromedriver::Logger.filter_levels = %i[
severe
info
warning
debug
]
```

## Development
Expand Down
18 changes: 13 additions & 5 deletions lib/capybara/chromedriver/logger.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'capybara'
require 'selenium-webdriver'

require "capybara/chromedriver/logger/test_hooks"
require "capybara/chromedriver/logger/version"
require "capybara/chromedriver/logger/js_error"
require "capybara/chromedriver/logger/message"
require "capybara/chromedriver/logger/collector"
require 'capybara/chromedriver/logger/test_hooks'
require 'capybara/chromedriver/logger/version'
require 'capybara/chromedriver/logger/js_error'
require 'capybara/chromedriver/logger/message'
require 'capybara/chromedriver/logger/collector'

module Capybara
module Chromedriver
Expand All @@ -20,6 +20,14 @@ def filters=(filters)
@filters = filters
end

def filter_levels
@filter_levels || []
end

def filter_levels=(filters)
@filter_levels = filters && filters.map(&:upcase).map(&:to_s)
end

def raise_js_errors?
!!@raise_js_errors
end
Expand Down
28 changes: 15 additions & 13 deletions lib/capybara/chromedriver/logger/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module Capybara
module Chromedriver
module Logger
class Collector
def initialize(log_destination: $stdout, filters: nil)
@log_destination = log_destination
@filters = filters || Capybara::Chromedriver::Logger.filters
def initialize(options = {})
@errors = []
@log_destination = options[:log_destination] || $stdout
@filters = options[:filters] || Capybara::Chromedriver::Logger.filters
@filter_levels = options[:filter_levels] ||
Capybara::Chromedriver::Logger.filter_levels
end

def flush_and_check_errors!
Expand All @@ -24,8 +26,7 @@ def raise_errors_if_needed!
formatted_errors = errors.map(&:to_s)
error_list = formatted_errors.join("\n")

raise JsError,
"Got some JS errors during testing:\n\n#{error_list}"
raise JsError, "Got some JS errors during testing:\n\n#{error_list}"
end

def flush_logs!
Expand Down Expand Up @@ -55,20 +56,21 @@ def logs(type)
end

def should_filter?(message)
filters.any? { |filter| filter =~ message.message }
should_filter_by_level?(message) || should_filter_content?(message)
end

def errors
@errors
def should_filter_by_level?(message)
filter_levels.include?(message.level)
end

def filters
@filters
def should_filter_content?(message)
filters.any? { |filter| filter =~ message.message }
end

def log_destination
@log_destination
end
attr_reader :errors
attr_reader :filters
attr_reader :filter_levels
attr_reader :log_destination
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/capybara/chromedriver/logger/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
expect_log_message("A console log")
end

it "should ignore filtered severity types" do
Capybara::Chromedriver::Logger.filter_levels = %i[info]

visit "/info"

expect_to_have_inserted_element
logger.flush_and_check_errors!
expect_no_log_messages

Capybara::Chromedriver::Logger.filter_levels = nil
end

def expect_no_log_messages
expect(log_destination.string).to eq('')
end
Expand Down

0 comments on commit 54e9070

Please sign in to comment.