1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/spec/spec_helper.rb

135 lines
4.1 KiB
Ruby
Raw Normal View History

2016-03-07 16:52:19 -08:00
# frozen_string_literal: true
2018-02-28 16:11:41 -08:00
2018-07-10 14:18:39 -07:00
require 'rspec'
require 'rspec/expectations'
require 'capybara'
require 'capybara/rspec' # Required here instead of in rspec_spec to avoid RSpec deprecation warning
require 'capybara/spec/test_app'
require 'nokogiri'
2018-06-17 10:29:46 -07:00
Capybara.save_path = File.join(Dir.pwd, 'save_path_tmp')
module Capybara
module SpecHelper
class << self
def configure(config)
2016-10-04 11:10:29 -07:00
config.filter_run_excluding requires: method(:filter).to_proc
2012-11-28 21:08:54 +01:00
config.before { Capybara::SpecHelper.reset! }
config.after { Capybara::SpecHelper.reset! }
config.shared_context_metadata_behavior = :apply_to_host_groups
2012-11-28 21:08:54 +01:00
end
def reset!
Capybara.app = TestApp
Capybara.app_host = nil
Capybara.default_selector = :xpath
Capybara.default_max_wait_time = 1
Capybara.ignore_hidden_elements = true
2013-02-15 20:02:15 +01:00
Capybara.exact = false
Capybara.raise_server_errors = true
Capybara.visible_text_only = false
2013-02-15 20:32:05 +01:00
Capybara.match = :smart
Capybara.enable_aria_label = false
Capybara.enable_aria_role = false
Capybara.default_set_options = {}
2018-05-16 20:04:24 -07:00
Capybara.disable_animation = false
Capybara.test_id = nil
Capybara.predicates_wait = true
Capybara.default_normalize_ws = false
2018-12-06 14:23:35 -08:00
Capybara.allow_gumbo = true
Capybara.w3c_click_offset = false
reset_threadsafe
2012-11-28 21:08:54 +01:00
end
def filter(requires, metadata)
2018-05-14 14:30:34 -07:00
if requires && metadata[:capybara_skip]
2012-11-28 21:08:54 +01:00
requires.any? do |require|
metadata[:capybara_skip].include?(require)
end
2012-11-28 21:08:54 +01:00
else
false
end
end
def spec(name, *options, &block)
@specs ||= []
@specs << [name, options, block]
end
def run_specs(session, name, **options, &filter_block)
specs = @specs
2020-09-05 12:24:43 -07:00
RSpec.describe Capybara::Session, name, options do
include Capybara::SpecHelper
include Capybara::RSpecMatchers
2020-02-17 11:49:12 -08:00
before do |example|
@session = session
instance_exec(example, &filter_block) if filter_block
end
after do
session.reset_session!
end
before :each, psc: true do
2020-09-05 12:24:43 -07:00
SpecHelper.reset_threadsafe(bool: true, session: session)
end
after psc: true do
2020-09-05 12:24:43 -07:00
SpecHelper.reset_threadsafe(session: session)
end
before :each, :exact_false do
Capybara.exact = false
end
specs.each do |spec_name, spec_options, block|
describe spec_name, *spec_options do # rubocop:disable RSpec/EmptyExampleGroup
class_eval(&block)
end
end
end
end
2020-09-05 12:24:43 -07:00
def reset_threadsafe(bool: false, session: nil)
2020-07-06 22:34:20 -07:00
# Work around limit on when threadsafe can be changed
Capybara::Session.class_variable_set(:@@instance_created, false) # rubocop:disable Style/ClassVars
Capybara.threadsafe = bool
session = session.current_session if session.respond_to?(:current_session)
2018-05-10 13:20:23 -07:00
session&.instance_variable_set(:@config, nil)
end
2018-02-28 16:11:41 -08:00
end
def silence_stream(stream)
old_stream = stream.dup
2019-03-25 09:37:25 -07:00
stream.reopen(RbConfig::CONFIG['host_os'].match?(/rmswin|mingw/) ? 'NUL:' : '/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
end
2020-09-05 12:24:43 -07:00
def quietly(&block)
silence_stream($stdout) do
silence_stream($stderr, &block)
end
end
def extract_results(session)
expect(session).to have_xpath("//pre[@id='results']")
# YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
YAML.load Capybara::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
end
def be_an_invalid_element_error(session)
satisfy { |error| session.driver.invalid_element_errors.any? { |e| error.is_a? e } }
end
def with_os_path_separators(path)
Gem.win_platform? ? path.to_s.tr('/', '\\') : path.to_s
end
end
end
2020-09-05 12:24:43 -07:00
Dir["#{File.dirname(__FILE__)}/session/**/*.rb"].each { |file| require_relative file }