Class: Conformity::Form

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/conformity/form.rb

Constant Summary collapse

METHODS =
[:visit, :find, :fill_in, :select, :check, :uncheck, :choose,
:click_on, :wait]

Instance Method Summary collapse

Constructor Details

#initialize(host = '', &block) ⇒ Form

Returns a new instance of Form.



11
12
13
14
15
# File 'lib/conformity/form.rb', line 11

def initialize(host = '', &block)
  set_host(host)
  @success_conditions = SuccessConditions.new(Capybara.current_session)
  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/conformity/form.rb', line 51

def method_missing(name, *args, &block)
  if METHODS.include?(name)
    action = Action.new(name, args, block)

    case action.name
    when :fill_in then action.field.type = :text
    when :choose  then action.field.type = :radio
    when :select  then action.field.type = :select
    end

    actions << action
  else
    super
  end
end

Instance Method Details

#actionsObject



73
74
75
# File 'lib/conformity/form.rb', line 73

def actions
  @actions ||= []
end

#field(name, opts = {}) ⇒ Object



21
22
23
24
# File 'lib/conformity/form.rb', line 21

def field(name, opts = {})
  fields << Field.new(name, opts)
  fields.last
end

#field_namesObject



30
31
32
# File 'lib/conformity/form.rb', line 30

def field_names
  fields.map(&:name)
end

#fieldsObject



26
27
28
# File 'lib/conformity/form.rb', line 26

def fields
  @fields ||= []
end

#fillObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/conformity/form.rb', line 34

def fill
  Capybara.current_session.reset!

  begin
    actions.each(&:run)
  rescue => e
    raise FillError, "#{e.message}"
  end
  Capybara.page
end

#fill_with(*field_values) ⇒ Object



45
46
47
48
49
# File 'lib/conformity/form.rb', line 45

def fill_with(*field_values)
  val_enum = field_values.to_enum
  fields.each { |field| field.value = val_enum.next }
  fill
end

#select(*args, &block) ⇒ Object

When passing a block to iniatialize using instance_eval the Kernel#select method gets called instead of method_missing



69
70
71
# File 'lib/conformity/form.rb', line 69

def select(*args, &block)
  method_missing(:select, *args, &block)
end

#set_host(host) ⇒ Object



17
18
19
# File 'lib/conformity/form.rb', line 17

def set_host(host)
  Capybara.app_host = host
end