Class: Celerity::Element

Inherits:
Object
  • Object
show all
Includes:
Container, Exception
Defined in:
lib/celerity/element.rb,
lib/celerity/watir_compatibility.rb

Overview

Superclass for all HTML elements.

Constant Summary collapse

HTML_401_TRANSITIONAL =

HTML 4.01 Transitional DTD

{
  :core        => [:class, :id, :style, :title],
  :cell_halign => [:align, :char, :charoff],
  :cell_valign => [:valign],
  :i18n        => [:dir, :lang],
  :event       => [:onclick, :ondblclick, :onmousedown, :onmouseup, :onmouseover,
                   :onmousemove, :onmouseout, :onkeypress, :onkeydown, :onkeyup],
  :sloppy      => [:name, :value]
}
CELLHALIGN_ATTRIBUTES =
CELLVALIGN_ATTRIBUTES =
BASE_ATTRIBUTES =
HTML_401_TRANSITIONAL.values_at(:core, :i18n, :event, :sloppy).flatten
ATTRIBUTES =
BASE_ATTRIBUTES
TAGS =
[]
DEFAULT_HOW =
nil

Instance Attribute Summary collapse

Attributes included from Container

#browser

Instance Method Summary collapse

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #check_box, #checkboxes, #container=, #contains_text, #dd, #dds, #del, #dels, #div, #divs, #dl, #dls, #dt, #dts, #em, #ems, #file_field, #file_fields, #form, #forms, #frame, #frames, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #hidden, #hiddens, #image, #images, #ins, #inses, #inspect, #label, #labels, #li, #link, #links, #lis, #map, #maps, #meta, #metas, #ol, #ols, #option, #p, #pre, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #span, #spans, #strong, #strongs, #table, #tables, #tbodies, #tbody, #text_field, #text_fields, #tfoot, #tfoots, #th, #thead, #theads, #ths, #ul, #uls

Methods included from ShortInspect

#short_inspect

Constructor Details

#initialize(container, *args) ⇒ Element

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Element.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/celerity/element.rb', line 33

def initialize(container, *args)
  self.container = container

  case args.size
  when 2
    @conditions = { args[0] => args[1] }
  when 1
    if args.first.is_a? Hash
      @conditions = args.first
    elsif (how = self.class::DEFAULT_HOW)
      @conditions = { how => args.first }
    else
      raise ArgumentError, "wrong number of arguments (1 for 2)"
    end
  when 0
    @conditions = { :index => Celerity.index_offset }
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
  end

  @conditions.freeze
  @object = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ String

Dynamically get element attributes.

Returns:

  • (String)

    The resulting attribute.

Raises:

  • (NoMethodError)

    if the element doesn’t support this attribute.

See Also:



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/celerity/element.rb', line 266

def method_missing(meth, *args, &blk)
  assert_exists

  meth = selector_to_attribute(meth)

  if self.class::ATTRIBUTES.include?(meth) || (self.class == Element && @object.hasAttribute(meth.to_s))
    return @object.getAttribute(meth.to_s)
  end
  Log.warn "Element\#method_missing calling super with #{meth.inspect}"
  super
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



11
12
13
# File 'lib/celerity/element.rb', line 11

def container
  @container
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
60
# File 'lib/celerity/element.rb', line 57

def ==(other)
  return false unless other.kind_of? Element
  xpath == other.xpath
end

#assert_existsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally to ensure the element actually exists.

Raises:



175
176
177
178
179
180
# File 'lib/celerity/element.rb', line 175

def assert_exists
  locate unless @object
  unless @object
    raise UnknownObjectException, "Unable to locate #{self.class.name[/::(.*)$/, 1]}, using #{identifier_string}"
  end
end

#attribute_stringString

Returns A string representation of the element’s attributes.

Returns:

  • (String)

    A string representation of the element’s attributes.



237
238
239
240
241
242
243
244
245
246
# File 'lib/celerity/element.rb', line 237

def attribute_string
  assert_exists

  result = ''
  @object.getAttributes.each do |attribute|
    result << %Q{#{attribute.getName}="#{attribute.getValue}"}
  end

  result
end

#attribute_value(attribute) ⇒ String

Returns The value of the given attribute.

Parameters:

  • The (String, #to_s)

    attribute.

Returns:

  • (String)

    The value of the given attribute.



150
151
152
153
# File 'lib/celerity/element.rb', line 150

def attribute_value(attribute)
  assert_exists
  @object.getAttribute attribute.to_s
end

#exists?true, false Also known as: exist?, exists

Checks if the element exists.

Returns:

  • (true, false)


187
188
189
190
191
192
# File 'lib/celerity/element.rb', line 187

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

#fire_event(event_name) ⇒ Object

Fires the given event for this element



103
104
105
106
# File 'lib/celerity/element.rb', line 103

def fire_event(event_name)
  assert_exists
  @object.fireEvent(event_name.sub(/^on/, ''))
end

#focusObject

Sets the focus to this element.



83
84
85
86
# File 'lib/celerity/element.rb', line 83

def focus
  assert_exists
  @object.focus
end

#focused?Boolean

Returns true if this element is the currently focused element

Celerity-specific.

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/celerity/element.rb', line 94

def focused?
  assert_exists
  @object == browser.page.getFocusedElement
end

#javascript_objectObject

Returns a JavaScript object representing the receiver



131
132
133
134
# File 'lib/celerity/element.rb', line 131

def javascript_object
  assert_exists
  @object.getScriptObject
end

#locateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally. Find the element on the page.



113
114
115
# File 'lib/celerity/element.rb', line 113

def locate
  @object = ElementLocator.new(@container, self.class).find_by_conditions(@conditions)
end

#methods(*args) ⇒ Object



278
279
280
281
282
# File 'lib/celerity/element.rb', line 278

def methods(*args)
  ms = super
  ms += self.class::ATTRIBUTES.map { |e| e.to_s }
  ms.sort
end

#objectObject

Returns the HtmlUnit object backing this element



121
122
123
# File 'lib/celerity/element.rb', line 121

def object
  @object || locate
end

#parentCelerity::Element?

Get the parent element

Returns:

  • (Celerity::Element, nil)

    subclass of Celerity::Element, or nil if no parent was found



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/celerity/element.rb', line 67

def parent
  assert_exists

  obj = @object.parentNode
  until element_class = Celerity::Util.htmlunit2celerity(obj.class)
    return nil if obj.nil?
    obj = obj.parentNode
  end

  element_class.new(@container, :object, obj)
end

#respond_to?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
287
288
# File 'lib/celerity/element.rb', line 284

def respond_to?(meth, include_private = false)
  meth = selector_to_attribute(meth)
  return true if self.class::ATTRIBUTES.include?(meth)
  super
end

#textString Also known as: innerText, inner_text

Return a text representation of the element as it would appear in a browser.

Returns:

  • (String)

See Also:



202
203
204
205
# File 'lib/celerity/element.rb', line 202

def text
  assert_exists
  @object.asText.strip # this must behave like ElementLocator
end

#to_sString

Returns A string representation of the element.

Returns:

  • (String)

    A string representation of the element.



140
141
142
143
# File 'lib/celerity/element.rb', line 140

def to_s
  assert_exists
  Celerity::Util.create_string @object
end

#to_xmlString Also known as: asXml, as_xml, html

Returns The normative XML representation of the element (including children).

Returns:

  • (String)

    The normative XML representation of the element (including children).



225
226
227
228
# File 'lib/celerity/element.rb', line 225

def to_xml
  assert_exists
  @object.asXml
end

#visible?boolean

Check if the element is visible to the user or not. Note that this only takes the _style attribute_ of the element (and its parents) into account - styles from applied CSS is not considered.

Returns:

  • (boolean)


163
164
165
166
# File 'lib/celerity/element.rb', line 163

def visible?
  assert_exists
  @object.isDisplayed
end

#xpathObject

return the canonical xpath for this element (Celerity-specific)



252
253
254
255
# File 'lib/celerity/element.rb', line 252

def xpath
  assert_exists
  @object.getCanonicalXPath
end