Module: SeleniumPlus::Elements::Table

Included in:
Selenium::WebDriver::Element
Defined in:
lib/selenium_plus/selenium/table.rb

Instance Method Summary collapse

Instance Method Details

#headersArray<Selenium::WebDriver::Element>

Elements of table header

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/selenium_plus/selenium/table.rb', line 39

def headers
  th_size = table_raw.first.select{ |cell| cell.tag_name == 'th'}.size
  case
    when th_size == 1 # vertical header
      table_raw.map { |cell| cell[0] }
    when th_size > 1 # horizontal header
      table_raw.first
    else
      nil # no th elements detected
  end
end

#headers_textArray<String>

Table headers text Examples:

A html table header with horizontal headers
| id | name |

and

A html table header with vertical headers
| id   |
| name |

get converted into the following:
#=> ['id', 'name']

Returns:

  • (Array<String>)


81
82
83
# File 'lib/selenium_plus/selenium/table.rb', line 81

def headers_text
  headers.map { |cell| cell.text.strip }
end

#rowsArray[Array<Selenium::WebDriver::Element>]

Elements of table rows

Returns:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/selenium_plus/selenium/table.rb', line 54

def rows
  th_size = table_raw.first.select{ |cell| cell.tag_name == 'th'}.size
  case
    when th_size == 1 # vertical header
      table_raw.map{ |row| row[1..-1] }.transpose
    when th_size > 1 # horizontal header
      table_raw[1..-1]
    else  # no header, raw is rows
      table_raw
  end
end

#rows_textArray[Array<String>]

Table body rows text Examples:

A html table body with horizontal headers
| 1  | alex  |
| 2  | smith |

and

A html table body with vertical headers
| 1    | 2     |
| alex | smith |

get converted into the following:
#=> [['1', 'alex'], ['2', 'smith']]

Returns:

  • (Array[Array<String>])


101
102
103
# File 'lib/selenium_plus/selenium/table.rb', line 101

def rows_text
  rows.map { |row| row.map { |cell| cell.text.strip } }
end

#table_hashesArray<Hash>

Converts this table into an Array of Hash where the keys of each Hash are the headers in the table. Examples:

A html table:
| id | name  | age |
| 1  | alex  | 10  |
| 2  | smith | 16  |

Gets converted into the following:
# => [{'id' => '1', 'name' => 'alex', 'age' => '10'}, {'id' => '2', 'name' => 'smith', 'age' => '16'}]

Returns:

  • (Array<Hash>)


116
117
118
# File 'lib/selenium_plus/selenium/table.rb', line 116

def table_hashes
  rows_text.map{ |row| Hash[*headers_text.zip(row).flatten] }
end

#table_rawArray[Array<Selenium::WebDriver::Element>]

Raw Selenium::WebDriver::Element of the table

Examples:

A html table:
| id | name  |
| 1  | alex  |
| 2  | smith |

gets converted into the following:
#=> [[Element, Element],[Element, Element], [Element, Element]]

Returns:



17
18
19
# File 'lib/selenium_plus/selenium/table.rb', line 17

def table_raw
  self.find_all(:css, 'tr').map{ |row| row.find_all(:css, 'th,td')}
end

#table_raw_textArray[Array<String>]

Raw text of this table Examples:

A html table:
| id | name  |
| 1  | alex  |
| 2  | smith |

gets converted into the following:
#=> [['id', 'name'],['1', 'alex'], ['2', 'smith']]

Returns:

  • (Array[Array<String>])


32
33
34
# File 'lib/selenium_plus/selenium/table.rb', line 32

def table_raw_text
  table_raw.map{ |row| row.map{ |cell| cell.text.strip } }
end