2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2012-10-30 09:30:45 -04:00
|
|
|
require "rspec"
|
2016-04-01 14:40:14 -04:00
|
|
|
require "rspec/expectations"
|
2012-10-30 09:30:45 -04:00
|
|
|
require "capybara"
|
|
|
|
require "capybara/rspec" # Required here instead of in rspec_spec to avoid RSpec deprecation warning
|
|
|
|
require "capybara/spec/test_app"
|
|
|
|
require "nokogiri"
|
2012-10-30 09:18:48 -04:00
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
# Alias be_truthy/be_falsey if not already defined to be able to use in RSpec 2 and 3
|
|
|
|
unless RSpec::Matchers.method_defined?(:be_truthy)
|
|
|
|
RSpec::Matchers.module_eval do
|
|
|
|
alias be_truthy be_true
|
|
|
|
alias be_falsey be_false
|
|
|
|
alias be_falsy be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
module Capybara
|
|
|
|
module SpecHelper
|
|
|
|
class << self
|
|
|
|
def configure(config)
|
2012-11-28 15:08:54 -05:00
|
|
|
config.filter_run_excluding :requires => method(:filter).to_proc
|
|
|
|
config.before { Capybara::SpecHelper.reset! }
|
|
|
|
config.after { Capybara::SpecHelper.reset! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset!
|
|
|
|
Capybara.app = TestApp
|
|
|
|
Capybara.app_host = nil
|
|
|
|
Capybara.default_selector = :xpath
|
2015-04-14 16:56:30 -04:00
|
|
|
Capybara.default_max_wait_time = 1
|
2013-02-15 16:09:40 -05:00
|
|
|
Capybara.ignore_hidden_elements = true
|
2013-02-15 14:02:15 -05:00
|
|
|
Capybara.exact = false
|
2013-02-16 04:20:50 -05:00
|
|
|
Capybara.exact_options = false
|
2013-02-16 05:04:40 -05:00
|
|
|
Capybara.raise_server_errors = true
|
2013-02-17 08:46:37 -05:00
|
|
|
Capybara.visible_text_only = false
|
2013-02-15 14:32:05 -05:00
|
|
|
Capybara.match = :smart
|
2015-04-21 13:52:49 -04:00
|
|
|
Capybara.wait_on_first_by_default = false
|
2012-11-28 15:08:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter(requires, metadata)
|
2013-11-14 12:43:36 -05:00
|
|
|
if requires and metadata[:capybara_skip]
|
2012-11-28 15:08:54 -05:00
|
|
|
requires.any? do |require|
|
2013-11-14 12:43:36 -05:00
|
|
|
metadata[:capybara_skip].include?(require)
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-11-28 15:08:54 -05:00
|
|
|
else
|
|
|
|
false
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spec(name, options={}, &block)
|
|
|
|
@specs ||= []
|
|
|
|
@specs << [name, options, block]
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_specs(session, name, options={})
|
|
|
|
specs = @specs
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::Session, name, options do
|
2012-07-21 16:44:10 -04:00
|
|
|
include Capybara::SpecHelper
|
2014-04-08 17:28:16 -04:00
|
|
|
include Capybara::RSpecMatchers
|
2012-07-21 16:44:10 -04:00
|
|
|
before do
|
|
|
|
@session = session
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
@session.reset_session!
|
|
|
|
end
|
2012-12-02 01:11:29 -05:00
|
|
|
specs.each do |spec_name, spec_options, block|
|
|
|
|
describe spec_name, spec_options do
|
2012-07-21 16:44:10 -04:00
|
|
|
class_eval(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # class << self
|
|
|
|
|
|
|
|
def silence_stream(stream)
|
|
|
|
old_stream = stream.dup
|
|
|
|
stream.reopen(RbConfig::CONFIG['host_os'] =~ /rmswin|mingw/ ? 'NUL:' : '/dev/null')
|
|
|
|
stream.sync = true
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
stream.reopen(old_stream)
|
|
|
|
end
|
|
|
|
|
|
|
|
def quietly
|
|
|
|
silence_stream(STDOUT) do
|
|
|
|
silence_stream(STDERR) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_results(session)
|
2015-07-21 20:41:47 -04:00
|
|
|
expect(session).to have_xpath("//pre[@id='results']")
|
2013-03-26 11:19:37 -04:00
|
|
|
YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 17:28:16 -04:00
|
|
|
Dir[File.dirname(__FILE__) + "/session/**/*.rb"].each { |file| require_relative file }
|