Class: OperaWatir::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/operawatir/collection.rb,
lib/operawatir/compat/collection.rb

Constant Summary collapse

ATTRIBUTE_ALIASES =

Aliases that Watir1 defines for certain attributes

{
  :url => :href,
  :class => :class_name,
  :tag => :tag_name
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, elms = nil) ⇒ Collection

Returns a new instance of Collection.



10
11
12
13
# File 'lib/operawatir/collection.rb', line 10

def initialize(parent, elms=nil)
  self.parent, self.selector = parent, OperaWatir::Selector.new(self)
  @_elms = elms
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

TODO

- Find tag if method exists in a list of HTML5 elements
- If ends in ? then check that all returned values are true
- Else, return attributes from elements


93
94
95
# File 'lib/operawatir/collection.rb', line 93

def method_missing(method, *args, &blk)
  map_or_return {|elm| elm.send(method, *args, &blk) }
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/operawatir/collection.rb', line 8

def parent
  @parent
end

#selectorObject

Returns the value of attribute selector.



8
9
10
# File 'lib/operawatir/collection.rb', line 8

def selector
  @selector
end

Instance Method Details

#+(other) ⇒ Object

Set union, used for joining complex finders (specifically for Watir1)



47
48
49
# File 'lib/operawatir/collection.rb', line 47

def +(other)
  self.class.new(parent, raw_elements + other.raw_elements)
end

#[](index) ⇒ OperaWatir::Collection

Gets the element at index, starting from 1 (i.e. [0] in a normal array is [1] here.

Parameters:

  • index (Fixnum)

    The index of the element to retreive

Returns:



73
74
75
# File 'lib/operawatir/compat/collection.rb', line 73

def [](n)
  self.class.new(self).tap {|c| c.selector.index(n) }
end

#add_selector_from_arguments(args, default_method) ⇒ OperaWatir::Selector

Creates a new selector based on the arguments given to the Watir 1 browser methods, e.g. browser.div(:id, ‘content’).

Parameters:

  • args (Array)

    The array of arguments passed to the Watir method.

  • default_method (Method)

    The attribute to use when only a value is provided, e.g. browser.div(‘content’).

Returns:



20
21
22
23
24
25
26
# File 'lib/operawatir/compat/collection.rb', line 20

def add_selector_from_arguments(args)
  if not args.empty?
    args.each do |arg|
      selector.attribute arg
    end
  end
end

#attr(name) ⇒ Object



65
66
67
# File 'lib/operawatir/collection.rb', line 65

def attr(name)
  raw_elements.first.attr(name)
end

#attrs(name) ⇒ Object



69
70
71
72
73
# File 'lib/operawatir/collection.rb', line 69

def attrs(name)
  raw_elements.map do |el|
    el.attr(name)
  end
end

#exist?Boolean Also known as: exists?

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/operawatir/collection.rb', line 23

def exist?
  !raw_elements.empty?
rescue OperaWatir::Exceptions::UnknownObjectException
  false
end

#idObject

Attributes



61
62
63
# File 'lib/operawatir/collection.rb', line 61

def id
  map_or_return {|elm| elm.id}
end

#raw_elementsObject

Public interface to elms, used in Selector



52
53
54
55
56
# File 'lib/operawatir/collection.rb', line 52

def raw_elements
  _elms.tap do |e|
    #raise(OperaWatir::Exceptions::UnknownObjectException) if e.empty?
  end
end

#select(option_text) ⇒ Object

This only applies to Watir 1. #to_s is in Watir 2 treated the same

way as Object#to_s (which will give you a Collection instance).

# Fetches the string representation of this collection.
#
# @return [String] the string representation of this collection
# @raise [OperaWatir::Exceptions::UnknownObjectException]
def to_s
  # This should return all of the attributes defined on each
  # element. We don't have support for that, so lets just
  # output the useful ones.
  raw_elements.map do |el|
    "tag:          #{el.tag_name.downcase}\n"+
    "  id:           #{el.id}\n" +
    "  class:        #{el.class_name}\n" +
    "  title:        #{el.title}\n" +
    "  text:         #{el.text}"
  end.join("\n")
end


104
105
106
# File 'lib/operawatir/compat/collection.rb', line 104

def select(option_text)
  option(:text => option_text).node.setSelected
end

#selected?(option_text) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/operawatir/compat/collection.rb', line 108

def selected?(option_text)
  option(:text => option_text).node.isSelected
end

#single?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/operawatir/collection.rb', line 30

def single?
  raw_elements.length == 1
end