Class: Dommy::Interaction::Locator
- Inherits:
-
Object
- Object
- Dommy::Interaction::Locator
- 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
-
#find_button(locator) ⇒ Object
A submit-capable button by text, value, id, name, or alt.
-
#find_field(locator) ⇒ Object
A form field by id, name, label text, placeholder, or aria-label.
- #find_link(locator) ⇒ Object
-
#find_option(select_el, value) ⇒ Object
The
-
#form_for(element) ⇒ Object
The form owning an element: an explicit
formattribute, else the nearest ancestor -
#initialize(document) ⇒ Locator
constructor
A new instance of Locator.
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 (locator) = @document.query_selector_all( "button, input[type='submit'], input[type='image'], input[type='button']" ) resolve_single(.select { |b| (b, locator) }, locator, noun: "button", available: -> { DomSummary.(@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_link(locator) ⇒ Object
27 28 29 30 |
# File 'lib/dommy/interaction/locator.rb', line 27 def find_link(locator) matches = @document.query_selector_all("a").select { |a| link_matches?(a, locator) } resolve_single(matches, locator, noun: "link", available: -> { DomSummary.link_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) = select_el..to_a .find { |o| o.text_content.strip == value.to_s } || .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 |