Class: Chop::Form

Inherits:
Struct
  • Object
show all
Defined in:
lib/chop/form.rb

Defined Under Namespace

Classes: Checkbox, Default, Field, FieldFinder, MultipleCheckbox, MultipleFile, MultipleSelect, Radio, Select, SingleFile, Textarea, ViaJavascript

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



5
6
7
# File 'lib/chop/form.rb', line 5

def path
  @path
end

#sessionObject

Returns the value of attribute session

Returns:

  • (Object)

    the current value of session



5
6
7
# File 'lib/chop/form.rb', line 5

def session
  @session
end

#tableObject

Returns the value of attribute table

Returns:

  • (Object)

    the current value of table



5
6
7
# File 'lib/chop/form.rb', line 5

def table
  @table
end

Class Method Details

.diff!(selector, table, session: Capybara.current_session, timeout: Capybara.default_max_wait_time, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chop/form.rb', line 10

def self.diff! selector, table, session: Capybara.current_session, timeout: Capybara.default_max_wait_time, &block
  errors = session.driver.invalid_element_errors + [Capybara::ElementNotFound, Cucumber::MultilineArgument::DataTable::Different]
  session.document.synchronize timeout, errors: errors do
    root = begin
      if selector.is_a?(Capybara::Node::Element)
        selector
      else
        session.find(selector, wait: timeout)
      end
    rescue Capybara::ElementNotFound
      raise unless @allow_not_found
      Node("")
    end

    actual = root.all(Field.combined_css_selector, allow_reload: true)
      .filter_map { |field_element| Field.from(session, field_element) }
      .select(&:should_include_in_diff?)
      .uniq { |field| field.field[:name] }
      .filter_map(&:to_diff_row)

    # Retry if form fields haven't loaded yet (e.g., turbo-frame still loading)
    if actual.empty? && !table.raw.flatten.empty?
      raise Capybara::ElementNotFound, "No form fields found in #{selector.inspect}"
    end

    block.call(actual, root) if block_given?
    table.diff! actual, surplus_row: false, misplaced_col: false
  end
end

.fill_in!(table, session: Capybara.current_session, path: "features/support/fixtures") ⇒ Object



6
7
8
# File 'lib/chop/form.rb', line 6

def self.fill_in! table, session: Capybara.current_session, path: "features/support/fixtures"
  new(table, session, path).fill_in!
end

Instance Method Details

#fill_in!Object



41
42
43
44
45
# File 'lib/chop/form.rb', line 41

def fill_in!
  table.rows_hash.each do |label, value|
    Field.for(session, label, value, path).fill_in!
  end
end