Skip to content

Commit

Permalink
[rb] update guards to minimize unnecessary execution and allow messages
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jul 14, 2020
1 parent 6788463 commit 8ad5109
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 19 deletions.
7 changes: 7 additions & 0 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ Metrics/AbcSize:
- 'lib/selenium/webdriver/support/color.rb'

Metrics/BlockLength:
Max: 18
Exclude:
- "**/*_spec.rb"
- "**/spec_helper.rb"
- 'spec/rspec_matchers.rb'
- 'selenium-webdriver.gemspec'

Metrics/ClassLength:
Expand Down Expand Up @@ -171,3 +174,7 @@ Style/SlicingWithRange:

Style/StringLiterals:
Enabled: false

Style/SignalException:
Exclude:
- 'spec/integration/selenium/webdriver/guard_spec.rb'
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Chrome
describe Driver, only: {driver: :chrome} do
describe Driver, exclusive: {driver: :chrome} do
it 'gets and sets network conditions' do
driver.network_conditions = {offline: false, latency: 56, throughput: 789}
expect(driver.network_conditions).to eq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Chrome
describe Options, only: {browser: :chrome} do
describe Options, exclusive: {browser: :chrome} do
subject(:options) { Options.new }

it 'passes emulated device correctly' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Chrome
describe Profile do
describe Profile, exclusive: {browser: :chrome} do
let(:profile) { Profile.new }

it 'adds an extension' do
Expand Down
2 changes: 1 addition & 1 deletion rb/spec/integration/selenium/webdriver/edge/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Edge
describe Driver, only: {driver: :edge} do
describe Driver, exclusive: {driver: :edge} do
it 'gets and sets network conditions' do
driver.network_conditions = {offline: false, latency: 56, throughput: 789}
expect(driver.network_conditions).to eq(
Expand Down
6 changes: 3 additions & 3 deletions rb/spec/integration/selenium/webdriver/edge/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

module Selenium
module WebDriver
module EdgeChrome
describe Options, only: {browser: :edge} do
module Edge
describe Options, exclusive: {browser: :edge} do
subject(:options) { Options.new }

it 'passes emulated device correctly' do
Expand Down Expand Up @@ -61,6 +61,6 @@ module EdgeChrome
# end
# end
end
end # Chrome
end # Edge
end # WebDriver
end # Selenium
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Edge
describe Profile do
describe Profile, exclusive: {browser: :edge} do
let(:profile) { Profile.new }

it 'adds an extension' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

module Selenium
module WebDriver
describe Firefox, only: {browser: %i[firefox]} do
describe Firefox, exclusive: {browser: :firefox} do
it 'creates default capabilities' do
create_driver! do |driver|
caps = driver.capabilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Firefox
describe Profile, only: {browser: %i[firefox]} do
describe Profile, exclusive: {browser: :firefox} do
let(:profile) { Profile.new }

def read_generated_prefs(from = nil)
Expand Down
103 changes: 103 additions & 0 deletions rb/spec/integration/selenium/webdriver/guard_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require_relative 'spec_helper'

module Selenium
module WebDriver
module SpecSupport
describe Guards, exclusive: {driver: :chrome} do
describe '#exclude' do
it 'ignores an unrecognized guard parameter', invalid: {browser: :chrome} do
# pass
end

it 'skips without running', exclude: {browser: :chrome} do
puts "This code will not get executed"
end
end

describe '#exclusive' do
it 'skips without running if it does not match', exclusive: {browser: :not_chrome} do
puts "This code will not get executed"
end

it 'does not guard if it does match', exclusive: {browser: :chrome} do
# pass
end
end

describe '#only' do
it 'guards when value does not match', only: {browser: :not_chrome} do
fail
end

it 'does not guard when value matches', only: {browser: :chrome} do
# pass
end
end

describe '#except' do
it 'guards when value matches and test fails', except: {browser: :chrome} do
fail
end

it 'does not guard when value does not match and test passes', except: {browser: :not_chrome} do
# pass
end
end

context 'when multiple guards' do
it 'guards if neither only nor except match and test fails', only: {browser: :not_chrome},
except: {browser: :not_chrome} do
fail
end

it 'guards if both only and except match', only: {browser: :chrome},
except: {browser: :chrome} do
fail
end

it 'guards if except matches and only does not', only: {browser: :not_chrome},
except: {browser: :chrome} do
fail
end

it 'does not guard if only matches and except does not', only: {browser: :chrome},
except: {browser: :not_chrome} do
# pass
end
end

context 'when array of hashes' do
it 'guards if any Hash value is satisfied', only: [{browser: :chrome}, {browser: :not_chrome}] do
fail
end
end

context 'guard messages on failing tests' do
it 'gives correct message with single only excludes', except: [{browser: :chrome, message: 'bug1'},
{browser: :not_chrome, message: 'bug2'}] do
fail
end
end
end
end # SpecSupport
end # WebDriver
end # Selenium
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Remote
describe Driver, only: {driver: :remote} do
describe Driver, exclusive: {driver: :remote} do
it 'should expose session_id' do
expect(driver.session_id).to be_kind_of(String)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

module Selenium
module WebDriver
describe Element, only: {driver: :remote} do
describe Element, exclusive: {driver: :remote} do
before do
driver.file_detector = ->(filename) { File.join(__dir__, filename) }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
module Safari
describe Driver, only: {driver: %i[safari safari_preview]} do
describe Driver, exclusive: {driver: %i[safari safari_preview]} do
it 'gets and sets permissions' do
driver.permissions = {'getUserMedia' => false}
expect(driver.permissions).to eq('getUserMedia' => false)
Expand Down
11 changes: 7 additions & 4 deletions rb/spec/integration/selenium/webdriver/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@

c.before do |example|
guards = WebDriver::SpecSupport::Guards.new(example)
if guards.exclude.any?
skip 'Bug Prevents Execution.'
elsif guards.except.satisfied.any? || guards.only.unsatisfied.any?
ENV['SKIP_PENDING'] ? skip('Skip Guarded Spec') : pending('Guarded.')

skip_guard = guards.exclusive.unsatisfied.first || guards.exclude.satisfied.first
skip skip_guard.message if skip_guard

matching_guard = guards.except.satisfied.first || guards.only.unsatisfied.first
if matching_guard
ENV['SKIP_PENDING'] ? skip(matching_guard.message) : pending(matching_guard.message)
end
end

Expand Down
8 changes: 6 additions & 2 deletions rb/spec/integration/selenium/webdriver/spec_support/guards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module SpecSupport
class Guards
include Enumerable

GUARD_TYPES = %i[except only exclude].freeze
GUARD_TYPES = %i[except only exclude exclusive].freeze

def initialize(example, guards = nil)
@example = example
Expand All @@ -43,7 +43,11 @@ def only
end

def exclude
self.class.new(@example, @guards.select(&:exclude?)).satisfied
self.class.new(@example, @guards.select(&:exclude?))
end

def exclusive
self.class.new(@example, @guards.select(&:exclusive?))
end

def satisfied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ module WebDriver
module SpecSupport
class Guards
class Guard

attr_reader :message

def initialize(guard, type)
@type = type

reason = guard.delete(:message) || 'no reason given.'
@message = if @type == :exclude
"Test not guarded because it breaks test run; #{reason}."
elsif @type == :exclusive
'Test does not apply to this configuration.'
else
"Test guarded; #{reason}."
end
@drivers = []
@browsers = []
@platforms = []
Expand All @@ -35,18 +46,26 @@ def initialize(guard, type)
expand_platforms(guard)
end

# Bug is present on all configurations specified
def except?
@type == :except
end

# Bug is present on all configurations not specified
def only?
@type == :only
end

# Bug is present on all configurations specified, but test can not be run because it breaks other tests
def exclude?
@type == :exclude
end

# Test only applies to configurations specified
def exclusive?
@type == :exclusive
end

def satisfied?
satisfies_driver? && satisfies_browser? && satisfies_platform? && satisfies_window_manager?
end
Expand Down

0 comments on commit 8ad5109

Please sign in to comment.