Class: Vapir::Firefox::Element

Inherits:
Object
  • Object
show all
Includes:
Element, Container
Defined in:
lib/vapir-firefox/element.rb

Overview

Base class for html elements. This is not a class that users would normally access.

Direct Known Subclasses

Area, Dd, Div, Dl, Dt, Em, Form, Frame, H1, H2, H3, H4, H5, H6, Image, InputElement, Label, Li, Link, Map, Ol, Option, P, Pre, Span, Strong, TBody, Table, TableCell, TableRow, Ul

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

#element_by_xpath, #element_object_by_xpath, #element_objects_by_xpath, #elements_by_xpath, #extra_for_contained, #innermost_by_node, #innermost_matching_visible_text, #visible_text_nodes

Constructor Details

#initialize(how, what, extra = {}) ⇒ Element

Creates new instance of Firefox::Element.



12
13
14
15
16
17
18
19
20
21
# File 'lib/vapir-firefox/element.rb', line 12

def initialize(how, what, extra={})
  @firefox_socket=extra[:firefox_socket]
  @firefox_socket||= (extra[:browser].firefox_socket if extra[:browser])
  @firefox_socket||= (extra[:container].firefox_socket if extra[:container])
  @firefox_socket||= (what.firefox_socket if how==:element_object)
  unless @firefox_socket
    raise RuntimeError, "No firefox socket given! Firefox elements need this (specified in the :firefox_socket key of the extra hash)"
  end
  default_initialize(how, what, extra)
end

Instance Attribute Details

#firefox_socketObject (readonly)

Returns the value of attribute firefox_socket.



23
24
25
# File 'lib/vapir-firefox/element.rb', line 23

def firefox_socket
  @firefox_socket
end

Class Method Details

.element_object_style(element_object, document_object) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/vapir-firefox/element.rb', line 247

def self.element_object_style(element_object, document_object)
  if element_object.nodeType==1 #element_object.instanceof(element_object.firefox_socket.Components.interfaces.nsIDOMDocument)
    document_object.defaultView.getComputedStyle(element_object, nil)
  else
    nil
  end
end

Instance Method Details

#click(options = {}) ⇒ Object

Fires the click event on this element.

Options:

  • :wait => true or false. If true, waits for the javascript call to return, and calls the #wait method. If false, does not wait for the javascript to return and does not call #wait. Default is the current config.wait value (which is by default true).

  • :highlight => true or false. Highlights the element while clicking if true. Default is true.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/vapir-firefox/element.rb', line 164

def click(options={})
  options={:wait => config.wait, :highlight => true}.merge(options)
  result=nil
  with_highlight(options) do
    assert_enabled if respond_to?(:assert_enabled)
    if options[:wait]
      [:mousedown, :mouseup, :click].each do |event_type|
        # javascript stuff responding to previous events can cause self to stop existing, so check at every subsequent step
        if exists?
          result=fire_event(event_type, options)
        else
          return result
        end
      end
      wait
    else
      mouse_down_event=create_event_object('mousedown', options)
      mouse_up_event=create_event_object('mouseup', options)
      click_event=create_event_object('click', options)
      content_window_object.setTimeout(firefox_socket.call_function(:element_object => element_object, :mouse_down_event => mouse_down_event, :mouse_up_event => mouse_up_event, :click_event => click_event) do 
      " return function()
        { element_object.dispatchEvent(mouse_down_event);
          element_object.dispatchEvent(mouse_up_event);
          element_object.dispatchEvent(click_event);
        };"
      end, 0)
    end
  end
  result
end

#click_no_wait(options = {}) ⇒ Object

calls #click with :wait option false. Takes options:

  • :highlight => true or false. Highlights the element while clicking if true. Default is true.



198
199
200
# File 'lib/vapir-firefox/element.rb', line 198

def click_no_wait(options={})
  with_config(:wait => false) { click(options) }
end

#current_style_objectObject Also known as: computed_style_object

currentStyle is IE; document.defaultView.getComputedStyle is mozilla.



47
48
49
# File 'lib/vapir-firefox/element.rb', line 47

def current_style_object # currentStyle is IE; document.defaultView.getComputedStyle is mozilla. 
  document_object.defaultView.getComputedStyle(element_object, nil)
end

#fire_event(event_type, options = {}) ⇒ Object

Fires the given event on this element. The given event name can be either of the form ‘onclick’ (for compatibility with IE) or just ‘click’ (can also be Symbol :onclick or :click) takes options:

  • :wait => true/false - (default true) whether to wait for the fire event to return, and call #wait (see #wait’s documentation). if false, fires the event in a setTimeout(click function, 0) in the browser.

  • :highlight => true/false - (default true) whether to highlight this Element when firing the event.

TODO: Provide ability to specify event parameters like keycode for key events, and click screen

coordinates for mouse events.


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vapir-firefox/element.rb', line 62

def fire_event(event_type, options={})
  options={:wait => config.wait, :highlight => true}.merge(options)
  with_highlight(options) do
    event=create_event_object(event_type, options)
    if !options[:wait]
      raise "need a content window on which to setTimeout if we are not waiting" unless content_window_object
      content_window_object.setTimeout(firefox_socket.call_function(:element_object => element_object, :event => event){ "return function(){ element_object.dispatchEvent(event) };" }, 0)
      nil
    else
      result=element_object.dispatchEvent(event)
      wait if exists?
      result
    end
  end
end

#outer_htmlObject Also known as: outerHTML



25
26
27
28
29
30
31
# File 'lib/vapir-firefox/element.rb', line 25

def outer_html
  temp_parent_element=document_object.createElement('div')
  temp_parent_element.appendChild(element_object.cloneNode(true))
  self_outer_html=temp_parent_element.innerHTML
  
  return self_outer_html
end

#visible?Boolean

Checks this element and its parents for display: none or visibility: hidden, these are the most common methods to hide an html element. Returns false if this seems to be hidden or a parent is hidden.

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/vapir-firefox/element.rb', line 210

def visible? 
  assert_exists do
    firefox_socket.call_function(:element_to_check => element_object, :document_object => document_object) do %Q(
      var really_visible=null;
      while(element_to_check) //&& !(element_to_check instanceof Components.interfaces.nsIDOMDocument)
      { var style = element_to_check.nodeType==1 ? document_object.defaultView.getComputedStyle(element_to_check, null) : null;
        if(style)
        { // only pay attention to the innermost definition that really defines visibility - one of 'hidden', 'collapse' (only for table elements), 
          // or 'visible'. ignore 'inherit'; keep looking upward. 
          // this makes it so that if we encounter an explicit 'visible', we don't pay attention to any 'hidden' further up. 
          // this style is inherited - may be pointless for firefox, but IE uses the 'inherited' value. not sure if/when ff does.
          var visibility=style && style.visibility;
          if(really_visible==null && visibility)
          { visibility=visibility.toLowerCase();
            if(visibility=='hidden' || visibility=='collapse')
            { really_visible=false;
              return false; // don't need to continue knowing it's not visible. 
            }
            else if(visibility=='visible')
            { really_visible=true; // we don't return true yet because a parent with display of 'none' can override 
            }
          }
          // check for display property. this is not inherited, and a parent with display of 'none' overrides an immediate visibility='visible' 
          var display=style && style.display;
          if(display && display.toLowerCase()=='none')
          { return false;
          }
        }
        element_to_check=element_to_check.parentNode;
      }
      return true;
    )
    end
  end
end

#wait(options = {}) ⇒ Object

Waits for the browser to finish loading, if it is loading. See Firefox#wait.



203
204
205
# File 'lib/vapir-firefox/element.rb', line 203

def wait(options={})
  @container.wait(options) if config.wait
end