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

TO_S_SIZE =

number of spaces that separate the property from the value in the create_string method

14
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, #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, #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.



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 36

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

#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:



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

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

#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:



160
161
162
163
164
165
# File 'lib/celerity/element.rb', line 160

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.



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

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.



135
136
137
138
# File 'lib/celerity/element.rb', line 135

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)


172
173
174
175
176
177
# File 'lib/celerity/element.rb', line 172

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

#fire_event(event_name) ⇒ Object

Fires the given event for this element



88
89
90
91
# File 'lib/celerity/element.rb', line 88

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

#focusObject

Sets the focus to this element.



79
80
81
82
# File 'lib/celerity/element.rb', line 79

def focus
  assert_exists
  @object.focus
end

#javascript_objectObject

Returns a JavaScript object representing the receiver



116
117
118
119
# File 'lib/celerity/element.rb', line 116

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.



98
99
100
# File 'lib/celerity/element.rb', line 98

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

#methods(*args) ⇒ Object



263
264
265
266
267
# File 'lib/celerity/element.rb', line 263

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



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

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



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

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)


269
270
271
272
273
# File 'lib/celerity/element.rb', line 269

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:



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

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.



125
126
127
128
# File 'lib/celerity/element.rb', line 125

def to_s
  assert_exists
  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).



210
211
212
213
# File 'lib/celerity/element.rb', line 210

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)


148
149
150
151
# File 'lib/celerity/element.rb', line 148

def visible?
  assert_exists
  @object.isDisplayed
end

#xpathObject

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



237
238
239
240
# File 'lib/celerity/element.rb', line 237

def xpath
  assert_exists
  @object.getCanonicalXPath
end