Class: Druid::Elements::Element

Inherits:
Object
  • Object
show all
Includes:
Assist, NestedElements
Defined in:
lib/druid/elements/element.rb

Overview

Contains functionality that is common across all elements

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assist

#button_for, #cell_for, #cell_text_for, #check_checkbox, #checkbox_checked?, #checkbox_for, #clear_radio, #click_button_for, #click_link_for, #div_for, #div_text_for, #file_field_for, #file_field_value_set, #form_for, #h1_for, #h1_text_for, #h2_for, #h2_text_for, #h3_for, #h3_text_for, #h4_for, #h4_text_for, #h5_for, #h5_text_for, #h6_for, #h6_text_for, #hidden_field_for, #hidden_field_value_for, #image_for, #link_for, #list_item_for, #list_item_text_for, #ordered_list_for, #paragraph_for, #paragraph_text_for, #radio_button_for, #radio_selected?, #select_list_for, #select_list_value_for, #select_list_value_set, #select_radio, #span_for, #span_text_for, #table_for, #text_area_for, #text_area_value_for, #text_area_value_set, #text_field_for, #text_field_value_for, #text_field_value_set, #uncheck_checkbox, #unordered_list_for

Methods included from NestedElements

#button_element, #cell_element, #checkbox_element, #div_element, #file_field_element, #form_element, #h1_element, #h2_element, #h3_element, #h4_element, #h5_element, #h6_element, #hidden_field_element, #image_element, #link_element, #list_item_element, #ordered_list_element, #paragraph_element, #radio_button_element, #select_list_element, #span_element, #table_element, #text_area_element, #text_field_element, #unordered_list_element

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



15
16
17
18
# File 'lib/druid/elements/element.rb', line 15

def initialize(element)
  @element = element
  @driver = @element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

delegate calls to driver element



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/druid/elements/element.rb', line 280

def method_missing(m, *args, &block)
  puts "*** DEPRECATION WARNING"
  puts "*** You are calling a method named #{m}."
  puts "*** This method does not exist in druid so it is being passed to the driver."
  puts "*** This feature will be removed in the near future."
  puts "*** Please change your code to call the correct druid method."
  puts "*** If you are using functionality that does not exist in druid please request it be added."
  unless element.respond_to?(m)
    raise NoMethodError, "undefined method `#{m}` for #{element.inspect}:#{element.class}"
  end
  element.__send__(m, *args, &block)
end

Instance Attribute Details

#driverObject

Returns the value of attribute driver.



13
14
15
# File 'lib/druid/elements/element.rb', line 13

def driver
  @driver
end

#elementObject

Returns the value of attribute element.



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

def element
  @element
end

Class Method Details

.attribute_expression(identifier) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/druid/elements/element.rb', line 51

def self.attribute_expression(identifier)
  identifier.map do |key, value|
    if value.kind_of?(Array)
      "(" + value.map { |v| equal_pair(key, v) }.join(" or ") + ")"
    else
      equal_pair(key, value)
    end
  end.join(" and ")
end

.build_xpath_for(identifier) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/druid/elements/element.rb', line 41

def self.build_xpath_for identifier
  tag_locator = identifier.delete(:tag_name)
  idx = identifier.delete(:index)
  identifier.delete(:tag_name)
  xpath = ".//#{tag_locator}"
  xpath << "[#{attribute_expression(identifier)}]" unless identifier.empty?
  xpath << "[#{idx+1}]" if idx
  xpath
end

.equal_pair(key, value) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/druid/elements/element.rb', line 61

def self.equal_pair(key, value)
  if key == :label
    "@id=//label[normalize-space()=#{xpath_string(value)}]/@for"
  else
    "#{lhs_for(key)}=#{xpath_string(value)}"
  end
end

.findersObject



97
98
99
# File 'lib/druid/elements/element.rb', line 97

def self.finders
  [:class, :id, :index, :name, :xpath]
end

.have_to_build_xpath(identifier) ⇒ Object



37
38
39
# File 'lib/druid/elements/element.rb', line 37

def self.have_to_build_xpath(identifier)
  ['table', 'span', 'div', 'td', 'li', 'ol', 'ul', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p'].include? identifier[:tag_name] and identifier[:name]
end

.identifier_for(identifier) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/druid/elements/element.rb', line 21

def self.identifier_for identifier
  if have_to_build_xpath(identifier)
    how = :xpath
    what = build_xpath_for(identifier)
    return how => what
  else
    all_identities = {}
    identifier.each do |key, value|
      each = {key => value}
      ident = identifier_for_element each, finders, mapping
      all_identities[ident.keys.first] = ident.values.first
    end
    all_identities
  end
end

.identifier_for_element(identifier, find_by, find_by_mapping) ⇒ Object



90
91
92
93
94
95
# File 'lib/druid/elements/element.rb', line 90

def self.identifier_for_element identifier, find_by, find_by_mapping
  how, what = identifier.keys.first, identifier.values.first
  return how => what if find_by.include? how
  return find_by_mapping[how] => what if find_by_mapping[how]
  return nil => what
end

.lhs_for(key) ⇒ Object



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

def self.lhs_for(key)
  case key
  when :text, 'text'
    'normalize-space()'
  when :href
    'normalize-space(@href)'
  else
    "@#{key.to_s.gsub("_", "-")}"
  end
end

.mappingObject



101
102
103
# File 'lib/druid/elements/element.rb', line 101

def self.mapping
  {}
end

.xpath_string(value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/druid/elements/element.rb', line 80

def self.xpath_string(value)
  if value.include? "'"
    parts = value.split("'", -1).map { |part| "'#{part}'" }
    string = parts.join(%{,"'",})
    "concat(#{string})"
  else
    "'#{value}'"
  end
end

Instance Method Details

#==(other) ⇒ Boolean

compare this element to another to determine if they are equal

Returns:

  • (Boolean)


139
140
141
# File 'lib/druid/elements/element.rb', line 139

def ==(other)
  element == other.element
end

#attribute(attribute_name) ⇒ String?

Get the value of the given attribute of the element

Parameters:

Returns:



157
158
159
# File 'lib/druid/elements/element.rb', line 157

def attribute(attribute_name)
  element.attribute_value attribute_name
end

#clearObject

clear the contents of the element



197
198
199
# File 'lib/druid/elements/element.rb', line 197

def clear
  element.clear
end

#clickObject

Click this element



164
165
166
# File 'lib/druid/elements/element.rb', line 164

def click
  element.click
end

#double_clickObject

double click this element



204
205
206
# File 'lib/druid/elements/element.rb', line 204

def double_click
  element.double_click
end

#enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/druid/elements/element.rb', line 118

def enabled?
  element.enabled?
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  element.exist?
end

#fire_event(event_name) ⇒ Object

Fire the provided event on the current element



218
219
220
# File 'lib/druid/elements/element.rb', line 218

def fire_event(event_name)
  element.fire_event(event_name)
end

#focusObject



222
223
224
# File 'lib/druid/elements/element.rb', line 222

def focus
  element.focus
end

#inspectObject



175
176
177
# File 'lib/druid/elements/element.rb', line 175

def inspect
  element.inspect
end

#parentObject

Returns parent element of current element.



228
229
230
231
232
233
# File 'lib/druid/elements/element.rb', line 228

def parent
  parent = element.parent
  type = element.type if parent.tag_name.to_sym == :input
  cls = Druid::Elements.element_class_for(parent.tag_name, type)
  cls.new(parent)
end

#right_clickObject

right click this element



211
212
213
# File 'lib/druid/elements/element.rb', line 211

def right_click
  element.right_click
end

#send_keys(*args) ⇒ Object

Send keystrokes to this element

Examples:

element.send_keys "foo"                    #=> value: 'foo'
element.send_keys "tet", :arrow_left, "s"  #=> value: 'test'
element.send_keys [:control, 'a'], :space  #=> value: ' '

Parameters:



190
191
192
# File 'lib/druid/elements/element.rb', line 190

def send_keys(*args)
  element.send_keys(*args)
end

#style(property) ⇒ Object

get the value of the given CSS property



171
172
173
# File 'lib/druid/elements/element.rb', line 171

def style(property)
  element.style property
end

#tag_nameString

Get the tag name of this element

Returns:



148
149
150
# File 'lib/druid/elements/element.rb', line 148

def tag_name
  element.tag_name
end

#textString

Get the text for the element

Returns:



110
111
112
# File 'lib/druid/elements/element.rb', line 110

def text
  element.text
end

#valueString

Get the value of this element

Returns:



131
132
133
# File 'lib/druid/elements/element.rb', line 131

def value
  element.value
end

#visible?Boolean

Returns:

  • (Boolean)


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

def visible?
  element.visible?
end

#wait_until(timeout = 5, message = nil, &block) ⇒ Object

Waits until the block returns true

Parameters:

  • (default (Integer)

    to:5) seconds to wait before timing out



274
275
276
# File 'lib/druid/elements/element.rb', line 274

def wait_until(timeout=5, message=nil, &block)
  Watir::Wait.until(timeout, message, &block)
end

#when_not_visible(timeout = 5) ⇒ Object

Waits until the element is not visible

Parameters:

  • (default (Integer)

    to:5) seconds to wait before timing out



262
263
264
265
266
267
# File 'lib/druid/elements/element.rb', line 262

def when_not_visible(timeout=5)
  Watir::Wait.while(timeout, "Element still visible after #{timeout} seconds") do
    visible?
  end
  self
end

#when_present(timeout = 5) ⇒ Object

Waits until the element is present

Parameters:

  • (default (Integer)

    to:5) seconds to wait before timing out



240
241
242
243
# File 'lib/druid/elements/element.rb', line 240

def when_present(timeout=5)
  element.wait_until_present(timeout)
  self
end

#when_visible(timeout = 5) ⇒ Object

Waits until the element is visible

Parameters:

  • (default (Interger)

    to:5) seconds to wait before timing out



250
251
252
253
254
255
# File 'lib/druid/elements/element.rb', line 250

def when_visible(timeout=5)
  Watir::Wait.until(timeout, "Element was not visible in #{timeout} seconds") do
    visible?
  end
  self
end