Class: Dommy::Interaction::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/interaction/locator.rb

Overview

Finds DOM elements within a document/scope by Capybara-style locators. Pure querying: it raises ElementNotFoundError / AmbiguousElementError but does not mutate the DOM or perform navigation. Shared by the Rack session and the standalone Browser.

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Locator

Returns a new instance of Locator.



10
11
12
# File 'lib/dommy/interaction/locator.rb', line 10

def initialize(document)
  @document = document
end

Instance Method Details

#find_button(locator) ⇒ Object

A submit-capable button by text, value, id, name, or alt.



33
34
35
36
37
38
39
# File 'lib/dommy/interaction/locator.rb', line 33

def find_button(locator)
  buttons = @document.query_selector_all(
    "button, input[type='submit'], input[type='image'], input[type='button']"
  )
  resolve_single(buttons.select { |b| button_matches?(b, locator) }, locator,
    noun: "button", available: -> { DomSummary.button_labels(@document) })
end

#find_field(locator) ⇒ Object

A form field by id, name, label text, placeholder, or aria-label.



15
16
17
18
19
20
21
22
23
24
# File 'lib/dommy/interaction/locator.rb', line 15

def find_field(locator)
  candidates = []
  by_id = element_by_id(locator)
  candidates << by_id if by_id
  candidates.concat(by_name(locator))
  candidates.concat(label_targets(locator))
  candidates.concat(by_field_attribute("placeholder", locator))
  candidates.concat(by_field_attribute("aria-label", locator))
  resolve_single(candidates, locator, noun: "field", available: -> { DomSummary.field_labels(@document) })
end

#find_option(select_el, value) ⇒ Object

The



42
43
44
45
46
# File 'lib/dommy/interaction/locator.rb', line 42

def find_option(select_el, value)
  options = select_el.options.to_a
  options.find { |o| o.text_content.strip == value.to_s } ||
    options.find { |o| (o.get_attribute("value") || "").to_s == value.to_s }
end

#form_for(element) ⇒ Object

The form owning an element: an explicit form attribute, else the nearest ancestor .



50
51
52
53
54
55
56
57
# File 'lib/dommy/interaction/locator.rb', line 50

def form_for(element)
  form_id = element.get_attribute("form")
  if form_id && !form_id.empty?
    element_by_id(form_id)
  else
    element.closest("form")
  end
end