Class: Chop::Form

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

Defined Under Namespace

Classes: Checkbox, Default, Field, MultipleCheckbox, MultipleFile, MultipleSelect, Radio, Select, SingleFile, 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, &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
39
40
# File 'lib/chop/form.rb', line 10

def self.diff! selector, table, session: Capybara.current_session, &block
  root = begin
    if selector.is_a?(Capybara::Node::Element)
      selector
    else
      session.find(selector)
    end
  rescue Capybara::ElementNotFound
    raise unless @allow_not_found
    Node("")
  end

  all_fields = root.all("input, textarea, select")
  relevant_fields = all_fields.inject([]) do |fields, field|
    next fields if field[:name].blank?
    next fields if field[:type] == "submit"
    next fields if field[:type] == "hidden"
    fields + [field]
  end
  deduplicated_fields = relevant_fields.inject([]) do |fields, field|
    next fields if fields.map { |field| field[:name] }.include?(field[:name])
    fields + [field]
  end
  actual = deduplicated_fields.inject([]) do |fields, field|
    next fields unless label = find_label_for(field)
    field = Field.from(session, field)
    fields + [[label.text(:all), field.get_value.to_s]]
  end
  block.call(actual, root) if block_given?
  table.diff! actual, surplus_row: false, misplaced_col: false
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

.find_label_for(field, session: Capybara.current_session) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/chop/form.rb', line 42

def self.find_label_for field, session: Capybara.current_session
  if field[:id].present?
    session.first("label[for='#{field[:id]}']", visible: :all, minimum: 0, wait: 0.1)
  else
    puts "cannot find label without id for #{field[:name]}"
  end
end

Instance Method Details

#fill_in!Object



50
51
52
53
54
# File 'lib/chop/form.rb', line 50

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