Class: Gamera::PageSections::Table
- Inherits:
-
Capybara::Node::Element
- Object
- Capybara::Node::Element
- Gamera::PageSections::Table
- 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
-
#delete_all_rows ⇒ Object
Delete all of the rows from the table.
-
#delete_row(name) ⇒ Object
Start the delete process for the row matching the specified name.
-
#edit_row(name) ⇒ Object
Start the edit process for the row matching the specified name.
-
#has_no_row?(name) ⇒ Boolean
Checks for the absence of a row with the given name.
-
#has_no_rows? ⇒ Boolean
Checks to see if the table has no rows.
-
#has_row?(name) ⇒ Boolean
Checks for the existence of a row with the given name.
-
#has_rows? ⇒ Boolean
Checks to see if the table has any rows.
-
#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
constructor
A new instance of Table.
-
#row_named(name) ⇒ row_class
Finds and returns a row from the table.
-
#rows ⇒ Array
Retrieves an array of rows from the table.
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.
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_rows ⇒ Object
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
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
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
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
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
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 |
#rows ⇒ Array
Retrieves an array of rows from the table
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 |