Class: Polonium::Element

Inherits:
Object
  • Object
show all
Includes:
WaitFor, ValuesMatch
Defined in:
lib/polonium/element.rb

Instance Attribute Summary collapse

Attributes included from WaitFor

#default_timeout

Instance Method Summary collapse

Methods included from WaitFor

#default_wait_for_time, #flunk, #time_class, #wait_for

Methods included from ValuesMatch

#values_match?

Constructor Details

#initialize(driver, locator) ⇒ Element

Returns a new instance of Element.



6
7
8
9
# File 'lib/polonium/element.rb', line 6

def initialize(driver, locator)
  @driver = driver
  @locator = locator
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object (protected)



202
203
204
205
206
207
208
209
# File 'lib/polonium/element.rb', line 202

def method_missing(method_name, *args, &blk)
  if driver.respond_to?(method_name)
    driver_args = [locator] + args
    driver.__send__(method_name, *driver_args, &blk)
  else
    super
  end
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



4
5
6
# File 'lib/polonium/element.rb', line 4

def driver
  @driver
end

#locatorObject (readonly)

Returns the value of attribute locator.



4
5
6
# File 'lib/polonium/element.rb', line 4

def locator
  @locator
end

Instance Method Details

#==(other) ⇒ Object



182
183
184
185
186
187
# File 'lib/polonium/element.rb', line 182

def ==(other)
  return false unless other.is_a?(Element)
  return false unless self.driver == other.driver
  return false unless self.locator == other.locator
  true
end

#assert_attribute(expected_name, expected_value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/polonium/element.rb', line 27

def assert_attribute(expected_name, expected_value)
  attr_locator = "#{locator}@#{expected_name}"
  wait_for_element do |configuration|
    actual = driver.get_attribute(attr_locator)  #todo: actual value
    configuration.message = "Expected attribute '#{attr_locator}' to be '#{expected_value}' but was '#{actual}'"
    values_match? actual, expected_value
  end
end

#assert_attribute_does_not_contain(attribute_name, illegal_value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/polonium/element.rb', line 36

def assert_attribute_does_not_contain(attribute_name, illegal_value)
  attr_locator = "#{locator}@#{attribute_name}"
  wait_for_element do |configuration|
    actual = driver.get_attribute(attr_locator)  #todo: actual value
    configuration.message = "Expected attribute '#{attr_locator}' to not contain '#{illegal_value}' but was '#{actual}'"
    !actual.match(illegal_value)
  end
end

#assert_checkedObject



71
72
73
74
75
# File 'lib/polonium/element.rb', line 71

def assert_checked
  wait_for_element(:message => "Expected '#{locator}' to be checked") do
    driver.is_checked(locator)
  end
end

#assert_contains(expected_text, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/polonium/element.rb', line 91

def assert_contains(expected_text, options={})
  return assert_contains_in_order(*expected_text) if expected_text.is_a? Array
  wait_for_element(options) do |configuration|
    if contains?(expected_text)
      true
    else
      configuration.message = "#{locator} should contain #{expected_text}"
      false
    end
  end
end

#assert_contains_in_order(*text_fragments) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/polonium/element.rb', line 117

def assert_contains_in_order(*text_fragments)
  wait_for_element do |configuration|
    success = false

    html = driver.get_text(locator)
    results = find_text_order_error_fragments(html, text_fragments)
    fragments_not_found = results[:fragments_not_found]
    fragments_out_of_order = results[:fragments_out_of_order]

    if !fragments_not_found.empty?
      configuration.message = "Certain fragments weren't found:\n" <<
                        "#{fragments_not_found.join("\n")}\n" <<
                        "\nhtml follows:\n #{html}\n"
    elsif !fragments_out_of_order.empty?
      configuration.message = "Certain fragments were out of order:\n" <<
                        "#{fragments_out_of_order.join("\n")}\n" <<
                        "\nhtml follows:\n #{html}\n"
    else
      success = true
    end

    success
  end
end

#assert_does_not_contain(expected_text, options = {}) ⇒ Object



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

def assert_does_not_contain(expected_text, options={})
  wait_for_element(options) do
    !contains?(expected_text)
  end
end

#assert_element_not_present(params = {}) ⇒ Object



15
16
17
# File 'lib/polonium/element.rb', line 15

def assert_element_not_present(params={})
  driver.assert_element_not_present(locator, params)
end

#assert_element_present(params = {}) ⇒ Object



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

def assert_element_present(params={})
  driver.assert_element_present(locator, params)
end

#assert_next_sibling(expected_sibling_id, options = {}) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/polonium/element.rb', line 109

def assert_next_sibling(expected_sibling_id, options = {})
  eval_js = "this.page().findElement('#{locator}').nextSibling.id"
  wait_for_element(:message => "id '#{locator}' should be next to '#{expected_sibling_id}'") do
    actual_sibling_id = driver.get_eval(eval_js)
    expected_sibling_id == actual_sibling_id
  end
end

#assert_not_checkedObject



77
78
79
80
81
# File 'lib/polonium/element.rb', line 77

def assert_not_checked
  wait_for_element(:message => "Expected '#{locator}' to not be checked") do
    !driver.is_checked(locator)
  end
end

#assert_not_visible(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/polonium/element.rb', line 62

def assert_not_visible(options={})
  options = {
    :message => "Expected '#{locator}' to be hidden, but it wasn't"
  }.merge(options)
  wait_for_element(options) do
    !driver.is_visible(locator)
  end
end

#assert_number_of_children(expected_number) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/polonium/element.rb', line 142

def assert_number_of_children(expected_number)
  eval_js = "this.page().findElement('#{locator}').childNodes.length"
  wait_for_element(:message => "id '#{locator}' should contain exactly #{expected_number} children") do
    actual_number = driver.get_eval(eval_js)
    expected_number == actual_number.to_i
  end
end

#assert_selected(expected_value) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/polonium/element.rb', line 45

def assert_selected(expected_value)
  wait_for_element do |configuration|
    actual = driver.get_selected_label(locator)
    configuration.message = "Expected '#{locator}' to be selected with '#{expected_value}' but was '#{actual}"
    values_match? actual, expected_value
  end
end

#assert_text(expected_text, options = {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/polonium/element.rb', line 83

def assert_text(expected_text, options={})
  wait_for_element(options) do |configuration|
    actual = driver.get_text(locator)
    configuration.message = "Expected text '#{expected_text}' to be full contents of #{locator} but was '#{actual}')"
    values_match? actual, expected_text
  end
end

#assert_value(expected_value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/polonium/element.rb', line 19

def assert_value(expected_value)
  wait_for_element do |configuration|
    actual_value = driver.get_value(locator)
    configuration.message = "Expected '#{locator}' to be '#{expected_value}' but was '#{actual_value}'"
    has_value? expected_value, actual_value
  end
end

#assert_visible(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/polonium/element.rb', line 53

def assert_visible(options={})
  options = {
    :message => "Expected '#{locator}' to be visible, but it wasn't"
  }.merge(options)
  wait_for_element(options) do
    driver.is_visible(locator)
  end
end

#clickObject



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

def click
  driver.click locator
end

#contains?(text) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/polonium/element.rb', line 178

def contains?(text)
  inner_html.match(text) ? true : false
end

#has_value?(expected_value, actual_value = driver.get_value(locator)) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/polonium/element.rb', line 170

def has_value?(expected_value, actual_value=driver.get_value(locator))
  expected_value == actual_value
end

#inner_htmlObject



174
175
176
# File 'lib/polonium/element.rb', line 174

def inner_html
  driver.get_inner_html(locator)
end

#is_not_present?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/polonium/element.rb', line 166

def is_not_present?
  !driver.is_element_present(locator)
end

#is_present?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/polonium/element.rb', line 162

def is_present?
  driver.is_element_present(locator)
end

#select(option_locator) ⇒ Object



158
159
160
# File 'lib/polonium/element.rb', line 158

def select(option_locator)
  driver.select(locator, option_locator)
end

#type(text) ⇒ Object



154
155
156
# File 'lib/polonium/element.rb', line 154

def type(text)
  driver.type(locator, text)
end