Class: Gamera::PageSections::Table

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

Overview

This class represents a table on a web page. For example, if you had a page like

<html>
  <body>
    <p>Example table</p>
    <table>
      <tr>
        <th>Item</th>
        <th>Wholesale Cost</th>
        <th>Retail Cost</th>
        <th></th>
        <th></th>
      </tr>
      <tr>
        <td>Red Hat</td>
        <td>2.00</td>
        <td>15.00</td>
        <td><a href="/item/12/edit">Edit</a></td>
        <td><a href="/item/12/delete">Delete</a></td>
      </tr>
      <tr>
        <td>Skull cap</td>
        <td>2.00</td>
        <td>27.00</td>
        <td><a href="/item/1/edit">Edit</a></td>
        <td><a href="/item/1/delete">Delete</a></td>
      </tr>
    </table>
  </body>
</html>

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

class HatPage < Gamera::Page
  include Forwardable
  include Gamera::PageSections

  attr_reader :registration_form, :table

  def initialize
    super(path_join(BASE_URL, '/hat'), %r{hat})

    headers = ['Item', 'Wholesale Cost', 'Retail Cost']
    @registration_form = Table.new(headers: headers, row_name:hat,
                                   name_column: 'Item')
    def_delegators :hat_table,
                   :hat, :hats,
                   :has_hat?, :has_hats?,
                   :edit_hat, :delete_hat
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(headers:, row_name:, plural_row_name: nil, name_column: 'Name', row_css: 'tr + tr', row_class: TableRow, row_editor: RowEditor.new, row_deleter: RowDeleter.new) ⇒ Table


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gamera/page_sections/table.rb', line 96

def initialize(headers:,
               row_name:,
               plural_row_name: nil,
               name_column: 'Name',
               row_css: 'tr + tr', # all tr's except the first one (which is almost always a table header)
               row_class: TableRow,
               row_editor: RowEditor.new,
               row_deleter: RowDeleter.new
              )
  @row_css = row_css
  @headers = headers
  @row_class = row_class
  @row_editor = row_editor
  @row_deleter = row_deleter
  @row_name = row_name
  @plural_row_name = plural_row_name
  @name_column = name_column.downcase.gsub(' ', '_').gsub(/[^a-z0-9_]+/, '')

  add_custom_function_names
end

Instance Method Details

#delete_all_rowsObject

Delete all of the rows from the table


154
155
156
157
158
159
160
# File 'lib/gamera/page_sections/table.rb', line 154

def delete_all_rows
  while has_rows?
    r = rows.first.send(name_column)
    delete_row(r)
    has_row?(r)
  end
end

#delete_row(name) ⇒ Object

Start the delete process for the row matching the specified name


163
164
165
# File 'lib/gamera/page_sections/table.rb', line 163

def delete_row(name)
  row_deleter.delete(row_named(name))
end

#edit_row(name) ⇒ Object

Start the edit process for the row matching the specified name


168
169
170
# File 'lib/gamera/page_sections/table.rb', line 168

def edit_row(name)
  row_editor.edit(row_named(name))
end

#has_row?(name) ⇒ Boolean

Checks for the existence of a row with the given name

otherwise


142
143
144
# File 'lib/gamera/page_sections/table.rb', line 142

def has_row?(name)
  page.has_selector?(row_css, text: name)
end

#has_rows?Boolean

Checks to see if the table has any rows


149
150
151
# File 'lib/gamera/page_sections/table.rb', line 149

def has_rows?
  has_selector?(row_css)
end

#row_named(name) ⇒ row_class

Finds and returns a row from the table


129
130
131
132
133
134
135
# File 'lib/gamera/page_sections/table.rb', line 129

def row_named(name)
  if name.is_a? String
    rows.detect { |r| r.send(name_column) == name } if has_row?(name)
  elsif name.is_a? Regexp
    rows.detect { |r| name.match r.send(name_column) } if has_row?(name)
  end
end

#rowsArray

Retrieves an array of rows from the table


120
121
122
123
# File 'lib/gamera/page_sections/table.rb', line 120

def rows
  has_rows?
  all(row_css).map { |r| row_class.new(r, headers) }
end