Module: Capybara::SpecHelper

Defined in:
lib/capybara/spec/spec_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(config) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/capybara/spec/spec_helper.rb', line 13

def configure(config)
  config.filter_run_excluding requires: method(:filter).to_proc
  config.before { Capybara::SpecHelper.reset! }
  config.after { Capybara::SpecHelper.reset! }
  # Test in 3.5+ where metadata doesn't autotrigger shared context inclusion - will be only behavior in RSpec 4
  config. = :apply_to_host_groups if RSpec::Core::Version::STRING.to_f >= 3.5
end

.filter(requires, metadata) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/capybara/spec/spec_helper.rb', line 37

def filter(requires, )
  if requires && [:capybara_skip]
    requires.any? do |require|
      [:capybara_skip].include?(require)
    end
  else
    false
  end
end

.reset!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/capybara/spec/spec_helper.rb', line 21

def reset!
  Capybara.app = TestApp
  Capybara.app_host = nil
  Capybara.default_selector = :xpath
  Capybara.default_max_wait_time = 1
  Capybara.ignore_hidden_elements = true
  Capybara.exact = false
  Capybara.raise_server_errors = true
  Capybara.visible_text_only = false
  Capybara.match = :smart
  Capybara.enable_aria_label = false
  Capybara.default_set_options = {}
  Capybara.disable_animation = false
  reset_threadsafe
end

.reset_threadsafe(bool = false, session = nil) ⇒ Object



87
88
89
90
91
92
# File 'lib/capybara/spec/spec_helper.rb', line 87

def reset_threadsafe(bool = false, session = nil)
  Capybara::Session.class_variable_set(:@@instance_created, false) # Work around limit on when threadsafe can be changed
  Capybara.threadsafe = bool
  session = session.current_session if session.respond_to?(:current_session)
  session&.instance_variable_set(:@config, nil)
end

.run_specs(session, name, **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/capybara/spec/spec_helper.rb', line 52

def run_specs(session, name, **options)
  specs = @specs
  RSpec.describe Capybara::Session, name, options do # rubocop:disable RSpec/EmptyExampleGroup
    include Capybara::SpecHelper
    include Capybara::RSpecMatchers
    # rubocop:disable RSpec/ScatteredSetup
    before do
      @session = session
    end

    after do
      session.reset_session!
    end

    before :each, psc: true do
      SpecHelper.reset_threadsafe(true, session)
    end

    after psc: true do
      SpecHelper.reset_threadsafe(false, session)
    end

    before :each, :exact_false do
      Capybara.exact = false
    end
    # rubocop:enable RSpec/ScatteredSetup

    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

.spec(name, *options, &block) ⇒ Object



47
48
49
50
# File 'lib/capybara/spec/spec_helper.rb', line 47

def spec(name, *options, &block)
  @specs ||= []
  @specs << [name, options, block]
end

Instance Method Details

#chrome?(session) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/capybara/spec/spec_helper.rb', line 129

def chrome?(session)
  session.respond_to?(:driver) && session.driver.respond_to?(:chrome?, true) && session.driver.send(:chrome?)
end

#chrome_gte?(version, session) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/capybara/spec/spec_helper.rb', line 137

def chrome_gte?(version, session)
  chrome?(session) && (session.driver.browser.capabilities[:version].to_f >= version)
end

#chrome_lt?(version, session) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/capybara/spec/spec_helper.rb', line 133

def chrome_lt?(version, session)
  chrome?(session) && (session.driver.browser.capabilities[:version].to_f < version)
end

#edge?(session) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/capybara/spec/spec_helper.rb', line 141

def edge?(session)
  session.respond_to?(:driver) && session.driver.respond_to?(:edge?, true) && session.driver.send(:edge?)
end

#extract_results(session) ⇒ Object



112
113
114
115
# File 'lib/capybara/spec/spec_helper.rb', line 112

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
end

#ie?(session) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/capybara/spec/spec_helper.rb', line 145

def ie?(session)
  session.respond_to?(:driver) && session.driver.respond_to?(:ie?, true) && session.driver.send(:ie?)
end

#marionette?(session) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/capybara/spec/spec_helper.rb', line 117

def marionette?(session)
  session.respond_to?(:driver) && session.driver.respond_to?(:marionette?, true) && session.driver.send(:marionette?)
end

#marionette_gte?(version, session) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/capybara/spec/spec_helper.rb', line 125

def marionette_gte?(version, session)
  marionette?(session) && (session.driver.browser.capabilities[:browser_version].to_f >= version)
end

#marionette_lt?(version, session) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/capybara/spec/spec_helper.rb', line 121

def marionette_lt?(version, session)
  marionette?(session) && (session.driver.browser.capabilities[:browser_version].to_f < version)
end

#quietlyObject



104
105
106
107
108
109
110
# File 'lib/capybara/spec/spec_helper.rb', line 104

def quietly
  silence_stream(STDOUT) do
    silence_stream(STDERR) do
      yield
    end
  end
end

#silence_stream(stream) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/capybara/spec/spec_helper.rb', line 95

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