# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819LEVELS = %w[warning info deprecated].freeze2021LEVELS.each do |level|22RSpec::Matchers.define "have_#{level}" do |entry|23match do |actual|24# Suppresses logging output to stderr while ensuring that it is still happening25default_output = Selenium::WebDriver.logger.io26io = StringIO.new27Selenium::WebDriver.logger.output = io2829begin30actual.call31rescue StandardError => e32raise e, 'Can not evaluate output when statement raises an exception'33ensure34Selenium::WebDriver.logger.output = default_output35end3637@entries_found = (io.rewind && io.read).scan(/\[:([^\]]*)\]/).flatten.map(&:to_sym)38expect(Array(entry).sort).to eq(@entries_found.sort)39end4041failure_message do42but_message = if @entries_found.nil? || @entries_found.empty?43"no #{entry} entries were reported"44else45"instead these entries were found: [#{@entries_found.join(', ')}]"46end47"expected :#{entry} to have been logged, but #{but_message}"48end4950failure_message_when_negated do51but_message = "it was found among these entries: [#{@entries_found.join(', ')}]"52"expected :#{entry} not to have been logged, but #{but_message}"53end5455def supports_block_expectations?56true57end58end59end606162