Class: Dommy::Rack::FieldInteractor

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/rack/field_interactor.rb

Overview

Drives form fields in the current document: fills text inputs, toggles radios / checkboxes, selects options, attaches files. Pure DOM mutation —it locates fields via a Locator and mutates the live Dommy elements, but issues no requests (Session turns a subsequent submit into navigation).

Instance Method Summary collapse

Constructor Details

#initialize(finder, document) ⇒ FieldInteractor

Returns a new instance of FieldInteractor.



10
11
12
13
# File 'lib/dommy/rack/field_interactor.rb', line 10

def initialize(finder, document)
  @finder = finder
  @document = document
end

Instance Method Details

#attach_file(locator, path) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/dommy/rack/field_interactor.rb', line 40

def attach_file(locator, path)
  input = @finder.find_field(locator)
  raise FileNotFoundError, "no such file: #{path}" unless ::File.exist?(path)

  file = Dommy::File.new(
    [::File.binread(path)], ::File.basename(path), "type" => FileUpload.mime_type_for(path)
  )
  input.__driver_set_files__([file])
  input
end

#check(locator) ⇒ Object



28
29
30
31
32
# File 'lib/dommy/rack/field_interactor.rb', line 28

def check(locator)
  box = @finder.find_field(locator)
  box.checked = true
  box
end

#choose(locator) ⇒ Object



21
22
23
24
25
26
# File 'lib/dommy/rack/field_interactor.rb', line 21

def choose(locator)
  radio = @finder.find_field(locator)
  clear_radio_group(radio)
  radio.checked = true
  radio
end

#fill_in(locator, with:) ⇒ Object



15
16
17
18
19
# File 'lib/dommy/rack/field_interactor.rb', line 15

def fill_in(locator, with:)
  field = @finder.find_field(locator)
  field.value = with.to_s
  field
end

#select(value, from:) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/dommy/rack/field_interactor.rb', line 51

def select(value, from:)
  select_el = @finder.find_field(from)
  option = @finder.find_option(select_el, value)
  raise ElementNotFoundError, "no option #{value.inspect} in #{from.inspect}" unless option

  select_el.options.each { |o| o.remove_attribute("selected") } unless select_el.multiple
  option.set_attribute("selected", "")
  select_el
end

#uncheck(locator) ⇒ Object



34
35
36
37
38
# File 'lib/dommy/rack/field_interactor.rb', line 34

def uncheck(locator)
  box = @finder.find_field(locator)
  box.checked = false
  box
end

#unselect(value, from:) ⇒ Object



61
62
63
64
65
66
# File 'lib/dommy/rack/field_interactor.rb', line 61

def unselect(value, from:)
  select_el = @finder.find_field(from)
  option = @finder.find_option(select_el, value)
  option&.remove_attribute("selected")
  select_el
end