Path: blob/trunk/rb/lib/selenium/webdriver/support/guards.rb
1865 views
# 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.1819require_relative 'guards/guard_condition'20require_relative 'guards/guard'2122module Selenium23module WebDriver24module Support25class Guards26GUARD_TYPES = %i[except only exclude exclusive flaky].freeze2728attr_reader :messages29attr_accessor :bug_tracker3031def initialize(example, bug_tracker: '', conditions: nil)32@example = example33@bug_tracker = bug_tracker34@guard_conditions = conditions || []35@guards = collect_example_guards36@messages = {}37end3839def add_condition(name, condition = false, &block)40condition = false if condition.nil?41@guard_conditions << GuardCondition.new(name, condition, &block)42WebDriver.logger.debug "Running with Guard '#{name}' set to: #{condition}"43end4445def add_message(name, message)46@messages[name] = message47end4849def disposition50if !skipping_guard.nil?51[:skip, skipping_guard.message]52elsif !pending_guard.nil? && ENV.fetch('SKIP_PENDING', nil)53[:skip, pending_guard.message]54elsif !pending_guard.nil?55[:pending, pending_guard.message]56end57end5859def satisfied?(guard)60@guard_conditions.all? { |condition| condition.satisfied?(guard) }61end6263private6465def collect_example_guards66guards = []6768GUARD_TYPES.each do |guard_type|69example_group = @example.metadata[:example_group]70example_guards = [@example.metadata[guard_type], example_group[guard_type]]71while example_group[:parent_example_group]72example_group = example_group[:parent_example_group]73example_guards << example_group[guard_type]74end7576example_guards.flatten.uniq.compact.each do |example_guard|77guards << Guard.new(example_guard, guard_type, self)78end79end8081guards82end8384def skipping_guard85@guards.select(&:exclusive?).find { |guard| !satisfied?(guard) } ||86@guards.select(&:exclude?).find { |guard| satisfied?(guard) }87end8889def pending_guard90@guards.select(&:except?).find { |guard| satisfied?(guard) } ||91@guards.select(&:only?).find { |guard| !satisfied?(guard) }92end93end # Guards94end # Support95end # WebDriver96end # Selenium979899