Module: RWebSpec::WebDriver::Assert

Includes:
Test::Unit::Assertions
Included in:
AbstractWebPage, LoadTestHelper, RSpecHelper, TestScript, WebTestCase
Defined in:
lib/rwebspec-webdriver/assert.rb

Instance Method Summary collapse

Instance Method Details

#assert_button_not_present(button_id) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/rwebspec-webdriver/assert.rb', line 252

def assert_button_not_present(button_id)
  elem = safe_find_element_by_id(button_id)
  if elem && elem.tag_name == "button" then
    fail("Unexpected button with this id: #{button_id}")
  end
  return true
end

#assert_button_not_present_with_text(button_text) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/rwebspec-webdriver/assert.rb', line 271

def assert_button_not_present_with_text(button_text)
  input_buttons = find_elements(:xpath, "//input[@type = 'button' or @type = 'submit']")
  input_button_values = input_buttons.collect { |x| x.attribute('value') }
  if input_button_values.include?(button_text)
    fail("can't find the button with text: #{button_text}")
  else
    #TODO check for just <button tag
    return true
  end
end

#assert_button_present(button_id) ⇒ Object

Button



248
249
250
# File 'lib/rwebspec-webdriver/assert.rb', line 248

def assert_button_present(button_id)
  find_element(:id, button_id)
end

#assert_button_present_with_text(button_text) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/rwebspec-webdriver/assert.rb', line 260

def assert_button_present_with_text(button_text)
  input_buttons = find_elements(:xpath, "//input[@type = 'button' or @type = 'submit']")
  input_button_values = input_buttons.collect { |x| x.attribute('value') }
  if input_button_values.include?(button_text)
    return true
  else
    #TODO check for just <button tag
    fail("can't find the button with text: #{button_text}")
  end
end

#assert_checkbox_not_selected(checkbox_name) ⇒ Object Also known as: assert_checkbox_not_checked

Checkbox



105
106
107
108
109
# File 'lib/rwebspec-webdriver/assert.rb', line 105

def assert_checkbox_not_selected(checkbox_name)
  elem = find_element(:name, checkbox_name)
  fail "Checkbox with name:#{checkbox_name} not found" unless elem.tag_name == "input"
  assert !elem.selected?
end

#assert_checkbox_selected(checkbox_name) ⇒ Object Also known as: assert_checkbox_checked



113
114
115
116
117
# File 'lib/rwebspec-webdriver/assert.rb', line 113

def assert_checkbox_selected(checkbox_name)
  elem = find_element(:name, checkbox_name)
  fail "Checkbox with name:#{checkbox_name} not found" unless elem.tag_name == "input"
  assert elem.selected?
end

#assert_disabled(tag, element_id) ⇒ Object



337
338
339
340
# File 'lib/rwebspec-webdriver/assert.rb', line 337

def assert_disabled(tag, element_id)
  element = find_element(:id, element_id)
  assert !element.enabled?
end

#assert_enabled(tag, element_id) ⇒ Object



342
343
344
345
346
# File 'lib/rwebspec-webdriver/assert.rb', line 342

def assert_enabled(tag, element_id)
  element = find_element(:id, element_id)
  puts "XXX :#{element.tag_name}"
  assert element.enabled?
end

#assert_equals(expected, actual, msg = nil) ⇒ Object

General



285
286
287
# File 'lib/rwebspec-webdriver/assert.rb', line 285

def assert_equals(expected, actual, msg=nil)
  perform_assertion { assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg) }
end

#assert_exists(tag, element_id) ⇒ Object Also known as: assert_exists?, assert_element_exists

Check a HTML element exists or not Example:

assert_exists("label", "receipt_date")
assert_exists(:span, :receipt_date)


294
295
296
297
298
299
300
301
# File 'lib/rwebspec-webdriver/assert.rb', line 294

def assert_exists(tag, element_id)
  elem = find_element(:id, element_id)
  if elem
    return true
  else
    fail("Element '#{tag}' with id: '#{element_id}' not found")
  end
end

#assert_hidden(tag, element_id) ⇒ Object Also known as: assert_not_visible

Assert tag with element id is hidden?, example

assert_hidden(:div, "secret")
assert_hidden(:span, "secret_span")


330
331
332
333
# File 'lib/rwebspec-webdriver/assert.rb', line 330

def assert_hidden(tag, element_id)
  element = find_element(:id, element_id)
  perform_assertion { assert(!element.displayed?, "Element '#{tag}' with id: '#{element_id}' is visible") }
end


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rwebspec-webdriver/assert.rb', line 85

def assert_link_not_present_with_text(link_text, opts = {})
  begin
    if opts && opts[:partial]
      elem = @web_browser.find_element(:partial_link_text, link_text)
    else
      elem = @web_browser.find_element(:link_text, link_text)
    end
  rescue => e
    puts "Failed to find the link text :#{e}"
  end
  if elem
    fail("unexpected link (exact): #{link_text} found")
  else
    return true
  end
end

Assert a link containing specified text in the page

<a href="">Click Me</a>
assert_link_present_with_text("Click ") # =>


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rwebspec-webdriver/assert.rb', line 67

def assert_link_present_with_text(link_text, opts = {})
  begin
    if opts && opts[:partial]
      elem = @web_browser.find_element(:partial_link_text, link_text)
    else
      elem = @web_browser.find_element(:link_text, link_text)
    end
  rescue => e
    puts "Failed to find the link text :#{e}"
  end

  if elem
    return true
  else
    fail("can't find the link with text: #{link_text}")
  end
end

#assert_nil(actual, msg = "") ⇒ Object



14
15
16
# File 'lib/rwebspec-webdriver/assert.rb', line 14

def assert_nil(actual, msg="")
  perform_assertion { assert(actual.nil?, msg) }
end

#assert_not(condition, msg = "") ⇒ Object



10
11
12
# File 'lib/rwebspec-webdriver/assert.rb', line 10

def assert_not(condition, msg = "")
  perform_assertion { assert(!condition, msg) }
end

#assert_not_exists(tag, element_id) ⇒ Object Also known as: assert_not_exists?, assert_element_not_exists?



306
307
308
309
310
311
312
313
# File 'lib/rwebspec-webdriver/assert.rb', line 306

def assert_not_exists(tag, element_id)
  elem = find_element(:id, element_id)
  if elem
    fail("#{tag} with #{element_id} not expected there, but exists")
  else
    return true
  end
end

#assert_not_nil(actual, msg = "") ⇒ Object



18
19
20
# File 'lib/rwebspec-webdriver/assert.rb', line 18

def assert_not_nil(actual, msg="")
  perform_assertion { assert(!actual.nil?, msg) }
end

#assert_option_equals(select_name, option_label) ⇒ Object Also known as: assert_select_label, assert_menu_label



177
178
179
180
181
182
183
184
185
# File 'lib/rwebspec-webdriver/assert.rb', line 177

def assert_option_equals(select_name, option_label)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  selected_label_list = []
  options.each do |op|
    selected_label_list << op.text if op.selected?
  end
  assert selected_label_list.join(",").to_s == option_label
end

#assert_option_not_present(select_name, option_label) ⇒ Object Also known as: assert_select_label_not_present



136
137
138
139
140
141
142
143
144
145
# File 'lib/rwebspec-webdriver/assert.rb', line 136

def assert_option_not_present(select_name, option_label)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  option_labels= options.collect { |x| x.attribute('value') }
  if option_labels.include?(option_label)
    fail "Unexpected select option label #{option_label}"
  else
    return true
  end
end

#assert_option_present(select_name, option_label) ⇒ Object Also known as: assert_select_label_present, assert_menu_label_present



163
164
165
166
167
168
169
170
171
172
# File 'lib/rwebspec-webdriver/assert.rb', line 163

def assert_option_present(select_name, option_label)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  option_labels = options.collect { |x| x.text }
  if option_labels.include?(option_label)
    return true
  else
    fail "Unexpected select option label #{option_label}"
  end
end

#assert_option_value_equals(select_name, option_value) ⇒ Object Also known as: assert_select_value, assert_menu_value



190
191
192
193
194
195
196
197
198
# File 'lib/rwebspec-webdriver/assert.rb', line 190

def assert_option_value_equals(select_name, option_value)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  selected_value_list = []
  options.each do |op|
    selected_value_list << op.attribute('value') if op.selected?
  end
  assert selected_value_list.join(",").to_s == option_value
end

#assert_option_value_not_present(select_name, option_value) ⇒ Object Also known as: assert_select_value_not_present

select



123
124
125
126
127
128
129
130
131
132
# File 'lib/rwebspec-webdriver/assert.rb', line 123

def assert_option_value_not_present(select_name, option_value)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  option_values = options.collect { |x| x.attribute('value') }
  if option_values.include?(option_value)
    fail "Unexpected select option value #{option_value}"
  else
    return true
  end
end

#assert_option_value_present(select_name, option_value) ⇒ Object Also known as: assert_select_value_present, assert_menu_value_present



149
150
151
152
153
154
155
156
157
158
# File 'lib/rwebspec-webdriver/assert.rb', line 149

def assert_option_value_present(select_name, option_value)
  select_element = find_element(:name, select_name)
  options = select_element.find_elements(:xpath, ".//option")
  option_values = options.collect { |x| x.attribute('value') }
  if option_values.include?(option_value)
    return true
  else
    fail "Unexpected select option value #{option_value}"
  end
end

#assert_radio_option_not_present(radio_group, radio_option) ⇒ Object

radio_group is the name field, radio options ‘value’ field



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rwebspec-webdriver/assert.rb', line 207

def assert_radio_option_not_present(radio_group, radio_option)
  begin
    elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  rescue => e
  end

  if elem
    fail("unexpected radio option: " + radio_option + " found")
  else
    return true
  end
end

#assert_radio_option_not_selected(radio_group, radio_option) ⇒ Object Also known as: assert_radio_button_not_checked, assert_radio_option_not_checked



237
238
239
240
# File 'lib/rwebspec-webdriver/assert.rb', line 237

def assert_radio_option_not_selected(radio_group, radio_option)
  elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  assert !elem.selected?
end

#assert_radio_option_present(radio_group, radio_option) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/rwebspec-webdriver/assert.rb', line 220

def assert_radio_option_present(radio_group, radio_option)
  elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  if elem
    return true
  else
    fail("can't find the radio option : '#{radio_option}'")
  end
end

#assert_radio_option_selected(radio_group, radio_option) ⇒ Object Also known as: assert_radio_button_checked, assert_radio_option_checked



229
230
231
232
# File 'lib/rwebspec-webdriver/assert.rb', line 229

def assert_radio_option_selected(radio_group, radio_option)
  elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  assert elem.selected?
end

#assert_text_field_value(textfield_name, text) ⇒ Object

Assert a text field (with given name) has the value

<input id=“tid” name=“text1” value=“text already there” type=“text”>

assert_text_field_value(“text1”, “text already there”) => true



390
391
392
# File 'lib/rwebspec-webdriver/assert.rb', line 390

def assert_text_field_value(textfield_name, text)
  perform_assertion { assert_equal(text, text_field(:name, textfield_name).attribute('value') ) }
end

#assert_text_in_element(element_id, text) ⇒ Object

– Not tested




398
399
400
401
402
# File 'lib/rwebspec-webdriver/assert.rb', line 398

def assert_text_in_element(element_id, text)
  elem = element_by_id(element_id)
  assert_not_nil(elem.innerText, "element #{element_id} has no text")
  perform_assertion { assert(elem.innerText.include?(text), "the text #{text} not found in element #{element_id}") }
end

#assert_text_in_page_source(text) ⇒ Object

Assert text present in page source (html)

assert_text_in_page_source("<b>iTest2</b>  Cool") # <b>iTest2</b>  Cool


35
36
37
# File 'lib/rwebspec-webdriver/assert.rb', line 35

def assert_text_in_page_source(text)
  perform_assertion { assert((@web_browser.page_source.include? text), 'expected html: ' + text + ' not found') }
end

#assert_text_not_in_page_source(text) ⇒ Object

Assert text not present in page source (html)

assert_text_not_in_page_source("<b>iTest2</b>  Cool") # <b>iTest2</b>  Cool


41
42
43
# File 'lib/rwebspec-webdriver/assert.rb', line 41

def assert_text_not_in_page_source(text)
  perform_assertion { assert(!(@web_browser.page_source.include? text), 'expected html: ' + text + ' found') }
end

#assert_text_not_present(text) ⇒ Object

Assert text not present in page source (html)

assert_text_not_present("iTest2 Cool") # <b>iTest2</b>  Cool


53
54
55
# File 'lib/rwebspec-webdriver/assert.rb', line 53

def assert_text_not_present(text)
  perform_assertion { assert(!(@web_browser.text.include? text), 'expected text: ' + text + ' found') }
end

#assert_text_not_present_in_table(table_id, text, options = {:just_plain_text => false}) ⇒ Object Also known as: assert_text_not_in_table



378
379
380
# File 'lib/rwebspec-webdriver/assert.rb', line 378

def assert_text_not_present_in_table(table_id, text, options = {:just_plain_text => false})
  perform_assertion { assert_not(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}") }
end

#assert_text_present(text) ⇒ Object

Assert text present in page source (html)

assert_text_present("iTest2 Cool") # <b>iTest2</b>  Cool


47
48
49
# File 'lib/rwebspec-webdriver/assert.rb', line 47

def assert_text_present(text)
  perform_assertion { assert((@web_browser.text.include? text), 'expected text: ' + text + ' not found') }
end

#assert_text_present_in_table(table_id, text, options = {:just_plain_text => false}) ⇒ Object Also known as: assert_text_in_table

Assert given text appear inside a table (inside <table> tag like below)

<table id=“t1”>

<tbody>

<tr id="row_1">
  <td id="cell_1_1">A</td>
  <td id="cell_1_2">B</td>
</tr>
<tr id="row_2">
  <td id="cell_2_1">a</td>
  <td id="cell_2_2">b</td>
</tr>

</tbody>

</table>

The plain text view of above table

A B a b

Examples

assert_text_present_in_table("t1", ">A<")  # => true
assert_text_present_in_table("t1", ">A<", :just_plain_text => true)  # => false


372
373
374
# File 'lib/rwebspec-webdriver/assert.rb', line 372

def assert_text_present_in_table(table_id, text, options = {:just_plain_text => false})
  perform_assertion { assert(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}") }
end

#assert_title_equals(title) ⇒ Object Also known as: assert_title

assertions



27
28
29
# File 'lib/rwebspec-webdriver/assert.rb', line 27

def assert_title_equals(title)
  assert_equals(title, @web_browser.page_title)
end

#assert_visible(tag, element_id) ⇒ Object

Assert tag with element id is visible?, eg.

assert_visible(:div, "public_notice")
assert_visible(:span, "public_span")


322
323
324
325
# File 'lib/rwebspec-webdriver/assert.rb', line 322

def assert_visible(tag, element_id)
  element = find_element(:id, element_id)
  perform_assertion { assert(element.displayed?, "Element '#{tag}' with id: '#{element_id}' not visible") }
end

#fail(message) ⇒ Object



22
23
24
# File 'lib/rwebspec-webdriver/assert.rb', line 22

def fail(message)
  perform_assertion { assert(false, message) }
end