Class: Gamera::PageSections::Form

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/gamera/page_sections/form.rb

Overview

This class represents an html form on a web page. For example, if you had a page like

<html>
<body>
  <h2>Example form</h2>
  <form action='/user/register'>
    <label for="name">Name</label><input type="text" name="name" value="" id="name">
    <label for="email">Email</label><input type="text" name="email" value="" id="email">
    <label for="password">Password</label><input type="text" name="password" value="" id="password">

    <input type="button" name="Register" id="save_button">
    <input type="button" name="Cancel" id="cancel_button">
  </form>
</body>
</html>

you could include this in a page object class like so:

class RegistrationPage < Gamera::Page
  include Forwardable

  attr_reader :registration_form, :table

  def initialize
    super(path_join(BASE_URL, '/registration/new'), %r{registration/new$})

    form_fields = {
      name: 'Name',
      email: 'Email',
      password: 'Password'
    }

    @registration_form = Gamera::PageSections::Form.new(form_fields)
    def_delegators :registration_form, *registration_form.field_method_names
  end

  def register
    find_button('Register').click
  end

  def cancel
    find_button('Cancel').click
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ Form

Returns a new instance of Form.



83
84
85
86
87
# File 'lib/gamera/page_sections/form.rb', line 83

def initialize(fields)
  @fields = fields
  @field_method_names = []
  define_field_methods
end

Instance Attribute Details

#field_method_namesObject

Returns the value of attribute field_method_names.



81
82
83
# File 'lib/gamera/page_sections/form.rb', line 81

def field_method_names
  @field_method_names
end

#fieldsObject

Returns the value of attribute fields.



81
82
83
# File 'lib/gamera/page_sections/form.rb', line 81

def fields
  @fields
end

Instance Method Details

#fill_in_form(fields) ⇒ Object

Utility method to populate the form based on a hash of field names and values

Parameters:

  • fields (Hash)

    The keys are the [field_name]s and the values are the values to which the fields are to be set.



93
94
95
96
97
98
99
100
101
102
# File 'lib/gamera/page_sections/form.rb', line 93

def fill_in_form(fields)
  fields.each do |field, value|
    f = send("#{field}_field")
    if f.tag_name == 'select'
      Array(value).each { |val| f.select(val) }
    else
      f.set(value)
    end
  end
end