Class: Webrat::Element

Inherits:
Object show all
Defined in:
lib/cucumber/webrat/element_locator.rb

Instance Method Summary collapse

Instance Method Details

#table_from_dlObject

:nodoc:



38
39
40
41
42
43
44
45
46
# File 'lib/cucumber/webrat/element_locator.rb', line 38

def table_from_dl #:nodoc:
  Webrat::XML.css_search(@element, 'dt').map do |dt|
    next_node = dt.next_sibling
    while next_node.name != 'dd'
      next_node = next_node.next_sibling
    end
    [dt.inner_html, next_node.inner_html]
  end
end

#table_from_listObject

:nodoc:



48
49
50
51
52
# File 'lib/cucumber/webrat/element_locator.rb', line 48

def table_from_list #:nodoc:
  Webrat::XML.css_search(@element, 'li').map do |li|
    [li.inner_html]
  end
end

#table_from_tableObject

:nodoc:



27
28
29
30
31
32
33
34
35
36
# File 'lib/cucumber/webrat/element_locator.rb', line 27

def table_from_table #:nodoc:
  col_count = nil
  Webrat::XML.css_search(element, 'tr').map do |row|
    cols = Webrat::XML.css_search(row, 'th,td')
    col_count ||= cols.length
    cols[0...col_count].map do |col|
      col.inner_html
    end
  end
end

#to_tableObject Also known as: to_a

Returns an Array of Array of String where each String is a “cell” in the table-like structure represented by this Element.

Supported elements are table, dl, ol and ul. Different conversion strategies are used depending on the kind of element:

  • table : Each tr becomes a row. The innerHTML of each td or th inside becomes a cell. The number

    of columns is determined by the number of cells in the first row.
    
  • dl : Each dt becomes a row with 2 cells. The innerHTML of the dt itself and the next dd become cells.

  • ul or ol : Each li becomes a row with one cell, the innerHTML of the li.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cucumber/webrat/element_locator.rb', line 14

def to_table
  case element.name
  when 'table'
    table_from_table
  when 'dl'
    table_from_dl
  when /ul|ol/
    table_from_list
  else
    raise "#to_table not supported for #{element.name} elements"
  end
end