Class: Capybara::Dommy::Driver

Inherits:
Capybara::Driver::Base
  • Object
show all
Defined in:
lib/capybara/dommy/driver.rb

Overview

A Capybara driver backed by Dommy::Rack::Session. Implements the navigation / query / reset! parts of the Capybara::Driver::Base contract; element interaction lives in Capybara::Dommy::Node. JavaScript, screenshot, window, and modal methods are left to Driver::Base (which raises Capybara::NotSupportedByDriverError).

Constant Summary collapse

VISIBILITY_MODES =
%i[all html none].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, default_host: nil, follow_redirects: nil, max_redirects: nil, visibility: nil) ⇒ Driver

Returns a new instance of Driver.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/capybara/dommy/driver.rb', line 15

def initialize(app,
               default_host: nil,
               follow_redirects: nil,
               max_redirects: nil,
               visibility: nil)
  super()
  config = Capybara::Dommy.configuration
  @app = app
  @visibility = visibility || config.visibility
  unless VISIBILITY_MODES.include?(@visibility)
    raise ArgumentError,
          "unknown visibility mode #{@visibility.inspect} (expected one of #{VISIBILITY_MODES.join(", ")})"
  end
  @raise_on_unsupported_js = config.raise_on_unsupported_js
  @session_options = {
    default_host: default_host || config.default_host,
    follow_redirects: follow_redirects.nil? ? config.follow_redirects : follow_redirects,
    max_redirects: max_redirects || config.max_redirects,
    # Capybara drives a trusted app and legitimately visits multiple
    # hosts (e.g. app_host / multi-server specs), so don't enforce origin.
    enforce_same_origin: false
  }
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



13
14
15
# File 'lib/capybara/dommy/driver.rb', line 13

def app
  @app
end

#visibilityObject (readonly)

Returns the value of attribute visibility.



13
14
15
# File 'lib/capybara/dommy/driver.rb', line 13

def visibility
  @visibility
end

Instance Method Details

#current_urlObject



62
63
64
# File 'lib/capybara/dommy/driver.rb', line 62

def current_url
  rack_session.current_url.to_s
end

#documentObject

— Node-facing seam (keeps the dommy-rack Session API in one place) —



108
109
110
# File 'lib/capybara/dommy/driver.rb', line 108

def document
  rack_session.document
end

#evaluate_async_script(_script, *_args) ⇒ Object



151
152
153
# File 'lib/capybara/dommy/driver.rb', line 151

def evaluate_async_script(_script, *_args)
  unsupported_js!("evaluate_async_script")
end

#evaluate_script(_script, *_args) ⇒ Object



147
148
149
# File 'lib/capybara/dommy/driver.rb', line 147

def evaluate_script(_script, *_args)
  unsupported_js!("evaluate_script")
end

#execute_script(_script, *_args) ⇒ Object

— JavaScript (unsupported) — When raise_on_unsupported_js is false these become no-ops, so tests that incidentally call them don’t fail.



143
144
145
# File 'lib/capybara/dommy/driver.rb', line 143

def execute_script(_script, *_args)
  unsupported_js!("execute_script")
end

#find_css(query, **_options) ⇒ Object

— Query (returns Capybara::Dommy::Node arrays) —



98
99
100
# File 'lib/capybara/dommy/driver.rb', line 98

def find_css(query, **_options)
  wrap(document&.query_selector_all(query))
end

#find_xpath(query, **_options) ⇒ Object



102
103
104
# File 'lib/capybara/dommy/driver.rb', line 102

def find_xpath(query, **_options)
  wrap(document&.xpath(query))
end


112
113
114
# File 'lib/capybara/dommy/driver.rb', line 112

def follow_link(element)
  rack_session.click_link_element(element)
end

#go_backObject



70
71
72
# File 'lib/capybara/dommy/driver.rb', line 70

def go_back
  rack_session.back
end

#go_forwardObject



74
75
76
# File 'lib/capybara/dommy/driver.rb', line 74

def go_forward
  rack_session.forward
end

#htmlObject

— Page state —



80
81
82
# File 'lib/capybara/dommy/driver.rb', line 80

def html
  rack_session.html
end

#invalid_element_errorsObject

Lets Capybara reload a node when it goes stale (after navigation).



135
136
137
# File 'lib/capybara/dommy/driver.rb', line 135

def invalid_element_errors
  [Capybara::Dommy::StaleElementReferenceError]
end

#needs_server?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/capybara/dommy/driver.rb', line 130

def needs_server?
  false
end

#rack_sessionObject

The dommy-rack session. Named ‘rack_session` to avoid colliding with Capybara::Driver::Base#session (the owning Capybara::Session). Rebuilt when the effective host (Capybara app_host / default_host) changes so current_url reflects it and same-origin checks pass.



43
44
45
46
47
48
49
50
# File 'lib/capybara/dommy/driver.rb', line 43

def rack_session
  host = effective_host
  if @rack_session.nil? || @rack_session_host != host
    @rack_session = ::Dommy::Rack::Session.new(@app, **@session_options.merge(default_host: host))
    @rack_session_host = host
  end
  @rack_session
end

#refreshObject



66
67
68
# File 'lib/capybara/dommy/driver.rb', line 66

def refresh
  rack_session.reload
end

#reset!Object

— Lifecycle —



122
123
124
# File 'lib/capybara/dommy/driver.rb', line 122

def reset!
  @rack_session = nil
end

#response_headersObject



92
93
94
# File 'lib/capybara/dommy/driver.rb', line 92

def response_headers
  rack_session.headers || {}
end

#status_codeObject



88
89
90
# File 'lib/capybara/dommy/driver.rb', line 88

def status_code
  rack_session.status
end

#submit_form(form, submitter:) ⇒ Object



116
117
118
# File 'lib/capybara/dommy/driver.rb', line 116

def submit_form(form, submitter:)
  rack_session.submit_form(form, submitter: submitter)
end

#titleObject



84
85
86
# File 'lib/capybara/dommy/driver.rb', line 84

def title
  document&.title
end

#visible?(element) ⇒ Boolean

Visibility decision used by Node#visible?. :all / :none treat every element as visible; :html defers to dommy-rack’s HTML-level check.

Returns:

  • (Boolean)


157
158
159
160
161
# File 'lib/capybara/dommy/driver.rb', line 157

def visible?(element)
  return true if @visibility == :all || @visibility == :none

  ::Dommy::Rack.visible?(element)
end

#visit(path) ⇒ Object

— Navigation —



54
55
56
57
58
59
60
# File 'lib/capybara/dommy/driver.rb', line 54

def visit(path)
  # A fresh visit resolves a relative path against the host root (not the
  # current page's directory), matching browser address-bar semantics.
  rack_session.visit(::URI.join("#{effective_host}/", path.to_s).to_s)
rescue URI::InvalidURIError
  rack_session.visit(path)
end

#wait?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/capybara/dommy/driver.rb', line 126

def wait?
  false
end