Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/rb/spec/rspec_matchers.rb
2884 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
LEVELS = %w[warning info deprecated].freeze
21
22
LEVELS.each do |level|
23
RSpec::Matchers.define "have_#{level}" do |entry|
24
match do |actual|
25
# Suppresses logging output to stderr while ensuring that it is still happening
26
default_output = Selenium::WebDriver.logger.io
27
io = StringIO.new
28
Selenium::WebDriver.logger.output = io
29
30
begin
31
actual.call
32
rescue StandardError => e
33
raise e, 'Can not evaluate output when statement raises an exception'
34
ensure
35
Selenium::WebDriver.logger.output = default_output
36
end
37
38
@entries_found = (io.rewind && io.read).scan(/\[:([^\]]*)\]/).flatten.map(&:to_sym)
39
expect(Array(entry).sort).to eq(@entries_found.sort)
40
end
41
42
failure_message do
43
but_message = if @entries_found.nil? || @entries_found.empty?
44
"no #{entry} entries were reported"
45
else
46
"instead these entries were found: [#{@entries_found.join(', ')}]"
47
end
48
"expected :#{entry} to have been logged, but #{but_message}"
49
end
50
51
failure_message_when_negated do
52
but_message = "it was found among these entries: [#{@entries_found.join(', ')}]"
53
"expected :#{entry} not to have been logged, but #{but_message}"
54
end
55
56
def supports_block_expectations?
57
true
58
end
59
end
60
end
61
62