Module: Vapir::Firefox::Container

Includes:
Container
Included in:
Element, PageContainer
Defined in:
lib/vapir-firefox/container.rb

Instance Method Summary collapse

Instance Method Details

#element_by_xpath(xpath) ⇒ Object

Returns the first element that matches the given xpath expression or query.



68
69
70
71
# File 'lib/vapir-firefox/container.rb', line 68

def element_by_xpath(xpath)
  # TODO: move this to common; should work the same for IE 
  base_element_class.factory(element_object_by_xpath(xpath), extra_for_contained)
end

#element_object_by_xpath(xpath) ⇒ Object

Returns the first element object that matches the given XPath query.

Refer: http://developer.mozilla.org/en/docs/DOM:document.evaluate


63
64
65
# File 'lib/vapir-firefox/container.rb', line 63

def element_object_by_xpath(xpath)
  document_object.evaluate(xpath, containing_object, nil, firefox_socket.Components.interfaces.nsIDOMXPathResult.attr('FIRST_ORDERED_NODE_TYPE'), nil).singleNodeValue
end

#element_objects_by_xpath(xpath) ⇒ Object

Returns array of element objects that match the given XPath query.

Refer: https://developer.mozilla.org/en/DOM/document.evaluate


53
54
55
56
57
58
59
# File 'lib/vapir-firefox/container.rb', line 53

def element_objects_by_xpath(xpath)
  elements=[]
  result=document_object.evaluate(xpath, containing_object, nil, firefox_socket.Components.interfaces.nsIDOMXPathResult.attr('ORDERED_NODE_SNAPSHOT_TYPE'), nil)
  return (0...result.snapshotLength).map do |i|
    result.snapshotItem(i)
  end
end

#elements_by_xpath(xpath) ⇒ Object

Returns the array of elements that match the given xpath query.



74
75
76
77
78
79
80
# File 'lib/vapir-firefox/container.rb', line 74

def elements_by_xpath(xpath)
  # TODO/FIX: shouldn't this return an ElementCollection? tests seem to expect it not to, addressing it with 0-based indexing, but that seems insconsistent with everything else. 
  # TODO: move this to common; should work the same for IE 
  element_objects_by_xpath(xpath).map do |element_object|
    base_element_class.factory(element_object, extra_for_contained)
  end
end

#extra_for_containedObject



46
47
48
# File 'lib/vapir-firefox/container.rb', line 46

def extra_for_contained
  base_extra_for_contained.merge(:firefox_socket => firefox_socket)
end

#innermost_by_node(match_proc_or_function) ⇒ Object

takes one argument, a proc or a JavascriptObject representing a function in javascript. this will be yielded successive dom nodes, and should return true if the node matches whatever criteria you care to match; false otherwise.

returns an ElementCollection consisting of the deepest elements within the dom heirarchy which match the given match_proc_or_function.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vapir-firefox/container.rb', line 98

def innermost_by_node(match_proc_or_function)
  if match_proc_or_function.is_a?(JavascriptObject)
    ElementCollection.new(self, base_element_class, extra_for_contained.merge(:candidates => proc do |container|
      firefox_socket.call_function(:match_function => match_proc_or_function, :containing_object => container.containing_object) do
        %Q(
          return Vapir.Ycomb(function(innermost_matching_nodes)
          { return function(container_node)
            { var child_nodes = $A(container_node.childNodes);
              var matched_child_elements = child_nodes.select(function(node){ return node.nodeType==1 && match_function(node); });
              if(matched_child_elements.length==0)
              { return [container_node];
              }
              else
              { return matched_child_elements.map(function(matched_child_element)
                { return innermost_matching_nodes(matched_child_element);
                }).inject([], function(a, b){ return a.concat(b); });
              }
            }
          })(containing_object);
        )
      end.to_array
    end))
  else
    base_innermost_by_node(match_proc_or_function)
  end
end

#innermost_matching_visible_text(text_or_regexp) ⇒ Object

takes text or regexp, and returns an ElementCollection consisting of deepest (innermost) elements in the dom heirarchy whose visible text matches what’s given (by substring for text; by regexp match for regexp)



127
128
129
130
131
132
133
134
135
# File 'lib/vapir-firefox/container.rb', line 127

def innermost_matching_visible_text(text_or_regexp)
  innermost_by_node(firefox_socket.call_function(:document_object => document_object, :text_or_regexp => text_or_regexp) do
    %Q(
      return function(node)
      { return Vapir.visible_text_nodes(node, document_object).join('').match(text_or_regexp);
      };
    )
  end.to_function)
end

#visible_text_nodesObject

returns a JavascriptObject representing an array of text nodes below this element in the DOM heirarchy which are visible - that is, their parent element is visible.

same as the Vapir::Common #visible_text_nodes implementation, but much much faster.



86
87
88
89
90
# File 'lib/vapir-firefox/container.rb', line 86

def visible_text_nodes
  assert_exists do
    visible_text_nodes_method.call(containing_object, document_object).to_array
  end
end