diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..d7b4dcd --- /dev/null +++ b/.rubocop.yml @@ -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 diff --git a/.travis.yml b/.travis.yml index 7f2a620..1bf508e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index 633efc2..22ca8de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/capybara/chromedriver/logger.rb b/lib/capybara/chromedriver/logger.rb index c798184..28a9b58 100644 --- a/lib/capybara/chromedriver/logger.rb +++ b/lib/capybara/chromedriver/logger.rb @@ -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 @@ -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 diff --git a/lib/capybara/chromedriver/logger/collector.rb b/lib/capybara/chromedriver/logger/collector.rb index 2176976..658ab94 100644 --- a/lib/capybara/chromedriver/logger/collector.rb +++ b/lib/capybara/chromedriver/logger/collector.rb @@ -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! @@ -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! @@ -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 diff --git a/spec/capybara/chromedriver/logger/collector_spec.rb b/spec/capybara/chromedriver/logger/collector_spec.rb index f8d1027..a9a1a69 100644 --- a/spec/capybara/chromedriver/logger/collector_spec.rb +++ b/spec/capybara/chromedriver/logger/collector_spec.rb @@ -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