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

Returns a new instance of Table.

Parameters:

  • headers (Array)

    An array of the strings from the tables header row

  • row_name (String)

    A label that can be used to create more readable versions of general row methods

  • plural_row_name (String) (defaults to: nil)

    Plural form of [row_name]

  • name_column (String) (defaults to: 'Name')

    The column heading for the column which contains each row’s name

  • row_css (String) (defaults to: 'tr + tr')

    The CSS selector which is used to find individual rows in the table

  • row_class (Class) (defaults to: TableRow)

    The class which will represent a table row

  • row_editor (Class) (defaults to: RowEditor.new)

    A class which defines the edit behavior for a row

  • row_deleter (Class) (defaults to: RowDeleter.new)

    A class which defines the edit behavior for a row



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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.tr(' ', '_').gsub(/[^a-z0-9_]+/, '')

  add_custom_function_names
end

Instance Method Details

#delete_all_rowsObject

Delete all of the rows from the table



169
170
171
172
173
174
175
# File 'lib/gamera/page_sections/table.rb', line 169

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



178
179
180
# File 'lib/gamera/page_sections/table.rb', line 178

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



183
184
185
# File 'lib/gamera/page_sections/table.rb', line 183

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

#has_no_row?(name) ⇒ Boolean

Checks for the absence of a row with the given name

otherwise

Parameters:

  • name (String)

    The name to look for in the table’s specified name column.

Returns:

  • (Boolean)

    False if a row with the specified name is found, true



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

def has_no_row?(name)
  page.has_no_selector?(row_css, text: name)
end

#has_no_rows?Boolean

Checks to see if the table has no rows

Returns:

  • (Boolean)

    False if the row selector is found, true otherwise



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

def has_no_rows?
  has_no_selector?(row_css)
end

#has_row?(name) ⇒ Boolean

Checks for the existence of a row with the given name

otherwise

Parameters:

  • name (String)

    The name to look for in the table’s specified name column.

Returns:

  • (Boolean)

    True if a row with the specified name is found, false



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

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

#has_rows?Boolean

Checks to see if the table has any rows

Returns:

  • (Boolean)

    True if the row selector is found, false otherwise



157
158
159
# File 'lib/gamera/page_sections/table.rb', line 157

def has_rows?
  has_selector?(row_css)
end

#row_named(name) ⇒ row_class

Finds and returns a row from the table

Parameters:

  • name (String)
    RegExp

    The name to look for in the table’s specified name column.

Returns:

  • (row_class)

    A row_class object that has the matching name or nil



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

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

Returns:

  • (Array)

    An array of row_class objects



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

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