Class: Seleniumrc::SeleniumElement

Inherits:
Object
  • Object
show all
Includes:
WaitFor
Defined in:
lib/seleniumrc/selenium_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

Constructor Details

#initialize(driver, locator) ⇒ SeleniumElement

Returns a new instance of SeleniumElement.



6
7
8
9
# File 'lib/seleniumrc/selenium_element.rb', line 6

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

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



4
5
6
# File 'lib/seleniumrc/selenium_element.rb', line 4

def driver
  @driver
end

#locatorObject (readonly)

Returns the value of attribute locator.



4
5
6
# File 'lib/seleniumrc/selenium_element.rb', line 4

def locator
  @locator
end

Instance Method Details

#==(other) ⇒ Object



154
155
156
157
158
159
# File 'lib/seleniumrc/selenium_element.rb', line 154

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

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



98
99
100
101
102
103
104
105
106
# File 'lib/seleniumrc/selenium_element.rb', line 98

def contains_text(expected_text, options={})
  is_present
  options = {
    :message => "#{locator} should contain #{expected_text}"
  }.merge(options)
  wait_for(options) do
    inner_html.include?(expected_text)
  end
end

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



108
109
110
111
112
113
# File 'lib/seleniumrc/selenium_element.rb', line 108

def does_not_contain_text(expected_text, options={})
  is_present
  wait_for(options) do
    !inner_html.include?(expected_text)
  end
end

#has_attribute(expected_value) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/seleniumrc/selenium_element.rb', line 37

def has_attribute(expected_value)
  is_present
  wait_for do |configuration|
    actual = driver.get_attribute(locator)  #todo: actual value
    configuration.message = "Expected attribute '#{locator}' to be '#{expected_value}' but was '#{actual}'"
    expected_value == actual
  end
end

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



115
116
117
118
119
120
121
122
# File 'lib/seleniumrc/selenium_element.rb', line 115

def has_next_sibling(expected_sibling_id, options = {})
  is_present
  eval_js = "this.page().findElement('#{locator}').nextSibling.id"
  wait_for(: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

#has_selected(expected_value) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/seleniumrc/selenium_element.rb', line 46

def has_selected(expected_value)
  is_present
  wait_for do |configuration|
    actual = driver.get_selected_label(locator)
    configuration.message = "Expected '#{locator}' to be selected with '#{expected_value}' but was '#{actual}"
    expected_value == actual
  end
end

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



89
90
91
92
93
94
95
96
# File 'lib/seleniumrc/selenium_element.rb', line 89

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

#has_text_in_order(*text_fragments) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/seleniumrc/selenium_element.rb', line 124

def has_text_in_order(*text_fragments)
  is_present
  wait_for 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

#has_value(expected_value) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/seleniumrc/selenium_element.rb', line 25

def has_value(expected_value)
  is_present
  wait_for 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

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/seleniumrc/selenium_element.rb', line 33

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

#inner_htmlObject



150
151
152
# File 'lib/seleniumrc/selenium_element.rb', line 150

def inner_html
  driver.get_inner_html(locator)
end

#is_checkedObject



75
76
77
78
79
80
# File 'lib/seleniumrc/selenium_element.rb', line 75

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

#is_not_checkedObject



82
83
84
85
86
87
# File 'lib/seleniumrc/selenium_element.rb', line 82

def is_not_checked
  is_present
  wait_for(:message => "Expected '#{locator}' to be checked") do
    !driver.is_checked(locator)
  end
end

#is_not_present(params = {}) ⇒ Object



18
19
20
# File 'lib/seleniumrc/selenium_element.rb', line 18

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

#is_not_present?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/seleniumrc/selenium_element.rb', line 21

def is_not_present?
  !driver.is_element_present(locator)
end

#is_not_visible(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/seleniumrc/selenium_element.rb', line 65

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

#is_present(params = {}) ⇒ Object



11
12
13
# File 'lib/seleniumrc/selenium_element.rb', line 11

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

#is_present?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/seleniumrc/selenium_element.rb', line 14

def is_present?
  driver.is_element_present(locator)
end

#is_visible(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/seleniumrc/selenium_element.rb', line 55

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