Class: WatirNokogiri::Element

Inherits:
Object
  • Object
show all
Extended by:
AttributeHelper
Includes:
Container, Exception
Defined in:
lib/watir-nokogiri/elements/element.rb

Overview

Base class for HTML elements.

Direct Known Subclasses

HTMLElement

Constant Summary

Constants included from AttributeHelper

AttributeHelper::IGNORED_ATTRIBUTES

Instance Method Summary collapse

Methods included from AttributeHelper

attribute_list, attributes, typed_attributes

Methods included from Container

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdi, #bdis, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #checkbox, #checkboxes, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #command, #commands, #data, #datalist, #datalists, #datas, #dd, #dds, #del, #dels, #details, #detailses, #dfn, #dfns, #dialog, #dialogs, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #em, #embed, #embeds, #ems, #extract_selector, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #form, #forms, #frame, #frames, #frameset, #framesets, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #head, #header, #headers, #heads, #hgroup, #hgroups, #hidden, #hiddens, #hr, #hrs, #htmls, #i, #iframe, #iframes, #image, #images, #img, #imgs, #input, #inputs, #ins, #inses, #is, #kbd, #kbds, #keygen, #keygens, #label, #labels, #legend, #legends, #li, #link, #links, #lis, #map, #maps, #mark, #marks, #menu, #menus, #meta, #metas, #meter, #meters, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radio, #radios, #rp, #rps, #rt, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #ss, #strong, #strongs, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #table, #tables, #tbody, #tbodys, #td, #tds, #text_field, #text_fields, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #title, #titles, #tr, #track, #tracks, #trs, #u, #ul, #uls, #us, #var, #vars, #video, #videos, #wbr, #wbrs

Constructor Details

#initialize(parent, selector) ⇒ Element

Returns a new instance of Element.



17
18
19
20
21
22
23
24
25
# File 'lib/watir-nokogiri/elements/element.rb', line 17

def initialize(parent, selector)
  @parent = parent
  @selector = selector
  @element = nil
  
  unless @selector.kind_of? Hash
    raise ArgumentError, "invalid argument: #{selector.inspect}"
  end			
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)



309
310
311
312
313
314
315
316
# File 'lib/watir-nokogiri/elements/element.rb', line 309

def method_missing(meth, *args, &blk)
  method = meth.to_s
  if method =~ /^data_(.+)$/
    attribute_value(method.gsub(/_/, '-'), *args)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if two elements are equal.

Examples:

html.a(:id => "foo") == html.a(:id => "foo")
#=> true


35
36
37
38
39
40
# File 'lib/watir-nokogiri/elements/element.rb', line 35

def ==(other)
  return false unless other.kind_of? self.class

  assert_exists
  @element == other.nokogiri
end

#attribute_value(attribute_name) ⇒ String

Returns given attribute value of element.

html.a(:id => “foo”).attribute_value “href” #=> “watir.com

Parameters:

  • attribute_name (String)

Returns:

  • (String)


54
55
56
57
# File 'lib/watir-nokogiri/elements/element.rb', line 54

def attribute_value(attribute_name)
  assert_exists
  @element.get_attribute(attribute_name) || ''
end

#click(*modifiers) ⇒ Object



59
60
61
62
# File 'lib/watir-nokogiri/elements/element.rb', line 59

def click(*modifiers)
  assert_exists
  raise "Not implemented"
end

#css_pathObject

Returns the css path for the element



68
69
70
71
# File 'lib/watir-nokogiri/elements/element.rb', line 68

def css_path()
  assert_exists
  @element.css_path
end

#documentObject

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.



77
78
79
# File 'lib/watir-nokogiri/elements/element.rb', line 77

def document
  @parent.document
end

#driverObject

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.



85
86
87
# File 'lib/watir-nokogiri/elements/element.rb', line 85

def driver
  @parent.driver
end

#exists?Boolean Also known as: exist?

Returns true if element exists.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
# File 'lib/watir-nokogiri/elements/element.rb', line 95

def exists?()
  begin
    assert_exists
    return true
  rescue UnknownObjectException, UnknownFrameException
    return false
  end
end

#htmlString

Returns outer (inner + element itself) HTML code of element.

html.div(:id => “foo”).html #=> “<div id="foo"><a>Click</a></div>”

Returns:

  • (String)


115
116
117
118
# File 'lib/watir-nokogiri/elements/element.rb', line 115

def html
  assert_exists
  @element.to_html
end

#idObject



120
121
122
123
# File 'lib/watir-nokogiri/elements/element.rb', line 120

def id()
  assert_exists
  attribute_value('id')
end

#inspectObject



125
126
127
128
129
130
131
# File 'lib/watir-nokogiri/elements/element.rb', line 125

def inspect()
  if @selector.has_key?(:element)
    '#<%s:0x%x located=%s selector=%s>' % [self.class, hash*2, !!@element, '{:element=>(nokogiri element)}']
  else
    '#<%s:0x%x located=%s selector=%s>' % [self.class, hash*2, !!@element, @selector.inspect]
  end
end

#nokogiriObject

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.



137
138
139
140
# File 'lib/watir-nokogiri/elements/element.rb', line 137

def nokogiri()
  assert_exists
  @element
end

#parentObject

Returns parent element of current element.



170
171
172
173
174
175
176
177
178
# File 'lib/watir-nokogiri/elements/element.rb', line 170

def parent
  assert_exists

  e = @element.parent

  if e.kind_of?(Nokogiri::XML::Element)
    WatirNokogiri.element_class_for(e.node_name.downcase).new(@parent, :element => e)
  end
end

#present?Boolean

Returns true if the element exists and is visible on the page.

Returns:

  • (Boolean)


186
187
188
189
# File 'lib/watir-nokogiri/elements/element.rb', line 186

def present?
  assert_exists
  raise "Not implemented"
end

#style(property = nil) ⇒ String

Returns given style property of this element.

html.a(:id => “foo”).style #=> “display: block” html.a(:id => “foo”).style “display” #=> “block”

Parameters:

  • property (String) (defaults to: nil)

Returns:

  • (String)


155
156
157
158
159
160
161
162
163
164
# File 'lib/watir-nokogiri/elements/element.rb', line 155

def style(property = nil)
  assert_exists
  styles = attribute_value('style').to_s.strip
  if property
    properties = Hash[styles.downcase.split(";").map { |p| p.split(":").map(&:strip) }]
    properties[property]				
  else
    styles
  end
end

#tag_nameString

Returns tag name of the element.

Returns:

  • (String)


197
198
199
200
# File 'lib/watir-nokogiri/elements/element.rb', line 197

def tag_name
  assert_exists
  @element.node_name.downcase
end

#textString

Returns the text of the element.

Returns:

  • (String)


208
209
210
211
# File 'lib/watir-nokogiri/elements/element.rb', line 208

def text()
  assert_exists
  @element.text.strip.gsub(/\s+/, ' ')
end

#to_subtypeObject

Cast this Element instance to a more specific subtype.

Examples:

browser.element(:xpath => "//input[@type='submit']").to_subtype
#=> #<WatirNokogiri::Button>


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/watir-nokogiri/elements/element.rb', line 221

def to_subtype
  elem = nokogiri()
  tag_name = elem.node_name.downcase

  klass = nil

  if tag_name == "input"
    klass = case elem.get_attribute(:type)
      when *Button::VALID_TYPES
        Button
      when 'checkbox'
        CheckBox
      when 'radio'
        Radio
      when 'file'
        FileField
      else
        TextField
      end
  else
    klass = WatirNokogiri.element_class_for(tag_name)
  end

  klass.new(@parent, :element => elem)
end

#valueString

Returns value of the element.

Returns:

  • (String)


253
254
255
256
# File 'lib/watir-nokogiri/elements/element.rb', line 253

def value
  assert_exists
  attribute_value('value')
end

#visible?Boolean

Returns true if this element is visible on the page.

Returns:

  • (Boolean)


264
265
266
267
# File 'lib/watir-nokogiri/elements/element.rb', line 264

def visible?
  assert_exists
  raise "Not implemented"
end

#xpathObject

Returns the xpath of the current element.



273
274
275
276
# File 'lib/watir-nokogiri/elements/element.rb', line 273

def xpath()
  assert_exists
  @element.path
end