Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/support/guards.rb
1865 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
require_relative 'guards/guard_condition'
21
require_relative 'guards/guard'
22
23
module Selenium
24
module WebDriver
25
module Support
26
class Guards
27
GUARD_TYPES = %i[except only exclude exclusive flaky].freeze
28
29
attr_reader :messages
30
attr_accessor :bug_tracker
31
32
def initialize(example, bug_tracker: '', conditions: nil)
33
@example = example
34
@bug_tracker = bug_tracker
35
@guard_conditions = conditions || []
36
@guards = collect_example_guards
37
@messages = {}
38
end
39
40
def add_condition(name, condition = false, &block)
41
condition = false if condition.nil?
42
@guard_conditions << GuardCondition.new(name, condition, &block)
43
WebDriver.logger.debug "Running with Guard '#{name}' set to: #{condition}"
44
end
45
46
def add_message(name, message)
47
@messages[name] = message
48
end
49
50
def disposition
51
if !skipping_guard.nil?
52
[:skip, skipping_guard.message]
53
elsif !pending_guard.nil? && ENV.fetch('SKIP_PENDING', nil)
54
[:skip, pending_guard.message]
55
elsif !pending_guard.nil?
56
[:pending, pending_guard.message]
57
end
58
end
59
60
def satisfied?(guard)
61
@guard_conditions.all? { |condition| condition.satisfied?(guard) }
62
end
63
64
private
65
66
def collect_example_guards
67
guards = []
68
69
GUARD_TYPES.each do |guard_type|
70
example_group = @example.metadata[:example_group]
71
example_guards = [@example.metadata[guard_type], example_group[guard_type]]
72
while example_group[:parent_example_group]
73
example_group = example_group[:parent_example_group]
74
example_guards << example_group[guard_type]
75
end
76
77
example_guards.flatten.uniq.compact.each do |example_guard|
78
guards << Guard.new(example_guard, guard_type, self)
79
end
80
end
81
82
guards
83
end
84
85
def skipping_guard
86
@guards.select(&:exclusive?).find { |guard| !satisfied?(guard) } ||
87
@guards.select(&:exclude?).find { |guard| satisfied?(guard) }
88
end
89
90
def pending_guard
91
@guards.select(&:except?).find { |guard| satisfied?(guard) } ||
92
@guards.select(&:only?).find { |guard| !satisfied?(guard) }
93
end
94
end # Guards
95
end # Support
96
end # WebDriver
97
end # Selenium
98
99