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.



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

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:



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

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

#identifier_string=(value) ⇒ Object

Sets the attribute identifier_string

Parameters:

  • value

    the value to set the attribute identifier_string to.



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

def identifier_string=(value)
  @identifier_string = value
end

Instance Method Details

#==(other) ⇒ Object



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

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:



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

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.



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

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.



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

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)


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

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

#fire_event(event_name) ⇒ Object

Fires the given event for this element



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

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

#focusObject

Sets the focus to this element.



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

def focus
  assert_exists
  @object.focus
end

#focused?Boolean

Returns true if this element is the currently focused element

Celerity-specific.

Returns:

  • (Boolean)


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

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

#javascript_objectObject

Returns a JavaScript object representing the receiver



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

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.



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

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

#methods(*args) ⇒ Object



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

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



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

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



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

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)


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

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:



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

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.



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

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).



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

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)


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

def visible?
  assert_exists
  @object.isDisplayed
end

#xpathObject

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



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

def xpath
  assert_exists
  @object.getCanonicalXPath
end