Class: Watir::TableRow

Inherits:
Element show all
Defined in:
lib/watir/table.rb

Constant Summary

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from Element

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #inspect, #ole_object, #ole_object=, #parent, #text, #to_s, #type_keys, #typingspeed, #visible?

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

#initialize(container, how, what) ⇒ TableRow

Returns an initialized instance of a table row

* o  - the object contained in the row
* container  - an instance of an IE object
* how          - symbol - how we access the row
* what         - what we use to access the row - id, index etc. If how is :ole_object then what is a Internet Explorer Raw Row


279
280
281
282
283
284
# File 'lib/watir/table.rb', line 279

def initialize(container, how, what)
  set_container container
  @how = how
  @what = what
  super nil
end

Instance Method Details

#[](index) ⇒ Object

Returns an element from the row as a TableCell object



293
294
295
296
297
298
299
# File 'lib/watir/table.rb', line 293

def [](index)
  assert_exists
  if @cells.length < index
    raise UnknownCellException, "Unable to locate a cell at index #{index}" 
  end
  return @cells[(index - 1)]
end

#column_countObject

defaults all missing methods to the array of elements, to be able to use the row as an array

def method_missing(aSymbol, *args)
    return @o.send(aSymbol, *args)
end


306
307
308
309
# File 'lib/watir/table.rb', line 306

def column_count
  locate
  @cells.length
end

#eachObject

this method iterates through each of the cells in the row. Yields a TableCell object



287
288
289
290
# File 'lib/watir/table.rb', line 287

def each
  locate
  0.upto(@cells.length-1) { |i| yield @cells[i] }
end

#locateObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/watir/table.rb', line 257

def locate
  @o = nil
  if @how == :ole_object
    @o = @what
  elsif @how == :xpath
    @o = @container.element_by_xpath(@what)
  else
    @o = @container.locate_tagged_element("TR", @how, @what)
  end
  if @o # cant call the assert_exists here, as an exists? method call will fail
    @cells = []
    @o.cells.each do |oo|
      @cells << TableCell.new(@container, :ole_object, oo)
    end
  end
end

#to_a(max_depth = 1) ⇒ Object

Returns (multi-dimensional) array of the cell texts in table’s row.

Works with th, td elements, colspan, rowspan and nested tables. Takes an optional parameter max_depth, which is by default 1



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/watir/table.rb', line 315

def to_a(max_depth=1)
  assert_exists
  y = []
  @o.cells.each do |cell|
    inner_tables = cell.getElementsByTagName("TABLE")
    inner_tables.each do |inner_table|
      # make sure that the inner table is directly child for this cell
      if inner_table?(cell, inner_table)
        max_depth -= 1
        y << Table.new(@container, :ole_object, inner_table).to_a(max_depth) if max_depth >= 1
      end
    end

    if inner_tables.length == 0
      y << cell.innerText.strip
    end
  end
  y
end