Class: Celerity::Element

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

Overview

Superclass for all HTML elements.

Constant Summary

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 =
HTML_401_TRANSITIONAL[:cell_halign]
CELLVALIGN_ATTRIBUTES =
HTML_401_TRANSITIONAL[:cell_valign]
BASE_ATTRIBUTES =
HTML_401_TRANSITIONAL.values_at(:core, :i18n, :event, :sloppy).flatten
ATTRIBUTES =
BASE_ATTRIBUTES
TAGS =
[]
DEFAULT_HOW =
nil

Attribute Summary

Method Summary

Methods included from Celerity::Container

#area, #areas, #button, #buttons, #cell, #cells, #check_box, #checkboxes, #container=, #contains_text, #dd, #dds, #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, #inspect, #label, #labels, #li, #link, #links, #lis, #map, #maps, #meta, #metas, #ol, #ols, #option, #p, #pre, #pres, #ps, #radio, #radios, #rescue_status_code_exception, #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 Celerity::ShortInspect

#short_inspect

Constructor Details

- (Element) initialize(container, *args)

A new instance of Element

Returns:

  • (Element) — 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
# 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
  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

- (String) method_missing(meth, *args, &blk)

Dynamically get element attributes.

Returns:

  • (String) — The resulting attribute.

Raises:

  • (NoMethodError) — if the element doesn’t support this attribute.

See Also:



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/celerity/element.rb', line 253

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

Attribute Details

- (Object) container (readonly)

Returns the value of attribute container



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

def container
  @container
end

Method Details

- (Object) ==(other)



55
56
57
58
# File 'lib/celerity/element.rb', line 55

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

- (Object) assert_exists

Used internally to ensure the element actually exists.

Raises:



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

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

- (String) attribute_string

A string representation of the element’s attributes.

Returns:

  • (String) — A string representation of the element’s attributes.


224
225
226
227
228
229
230
231
232
233
# File 'lib/celerity/element.rb', line 224

def attribute_string
  assert_exists

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

  result
end

- (String) attribute_value(attribute)

The value of the given attribute.

Parameters:

  • (String, #to_s) The — attribute.

Returns:

  • (String) — The value of the given attribute.


137
138
139
140
# File 'lib/celerity/element.rb', line 137

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

- (true) exists? Also known as: exists, exist?

Checks if the element exists.

Returns:

  • (true, false)


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

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

- (Object) fire_event(event_name)

Fires the given event for this element



90
91
92
93
# File 'lib/celerity/element.rb', line 90

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

- (Object) focus

Sets the focus to this element.



81
82
83
84
# File 'lib/celerity/element.rb', line 81

def focus
  assert_exists
  @object.focus
end

- (Object) javascript_object

Returns a JavaScript object representing the receiver



118
119
120
121
# File 'lib/celerity/element.rb', line 118

def javascript_object
  assert_exists
  @object.getScriptObject
end

- (Object) locate

Used internally. Find the element on the page.



100
101
102
# File 'lib/celerity/element.rb', line 100

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

- (Object) methods(*args)



265
266
267
268
269
# File 'lib/celerity/element.rb', line 265

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

- (Object) object

Returns the HtmlUnit object backing this element



108
109
110
# File 'lib/celerity/element.rb', line 108

def object
  @object || locate
end

- (Celerity::Element) parent

Get the parent element

Returns:

  • (Celerity::Element, nil) — subclass of Celerity::Element, or nil if no parent was found


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

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

- (Object) respond_to?(meth, include_private = false)



271
272
273
274
275
# File 'lib/celerity/element.rb', line 271

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

- (String) text Also known as: inner_text, innerText

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

Returns:

  • (String)

See Also:

  • inner_text


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

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

- (String) to_s

A string representation of the element.

Returns:

  • (String) — A string representation of the element.


127
128
129
130
# File 'lib/celerity/element.rb', line 127

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

- (String) to_xml Also known as: as_xml, asXml, html

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

Returns:

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


212
213
214
215
# File 'lib/celerity/element.rb', line 212

def to_xml
  assert_exists
  @object.asXml
end

- (boolean) visible?

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)


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

def visible?
  assert_exists
  @object.isDisplayed
end

- (Object) xpath

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



239
240
241
242
# File 'lib/celerity/element.rb', line 239

def xpath
  assert_exists
  @object.getCanonicalXPath
end