Module: RWebSpec::Assert

Included in:
AbstractWebPage, LoadTestHelper, RSpecHelper, TestScript, WebTestCase
Defined in:
lib/rwebspec-common/assert.rb

Instance Method Summary collapse

Instance Method Details

#assert(test, msg = nil) ⇒ Object

own assert method



6
7
8
9
10
11
12
13
14
# File 'lib/rwebspec-common/assert.rb', line 6

def assert test, msg = nil
  msg ||= "Failed assertion, no message given."
  # comment out self.assertions += 1 to counting assertions

  unless test then
    msg = msg.call if Proc === msg
    raise RWebSpec::Assertion, msg
  end
  true
end

#assert_button_not_present(button_id) ⇒ Object

Button



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

def assert_button_not_present(button_id)
  @web_browser.buttons.each { |button|
the_button_id = RWebSpec.framework == "Watir" ? button.id : button["id"]  
    perform_assertion { assert(the_button_id != button_id, "unexpected button id: #{button_id} found") }
  }
end

#assert_button_not_present_with_text(text) ⇒ Object



335
336
337
338
339
# File 'lib/rwebspec-common/assert.rb', line 335

def assert_button_not_present_with_text(text)
  @web_browser.buttons.each { |button|
    perform_assertion { assert(element_value(button) != text, "unexpected button id: #{text} found") }
  }
end

#assert_button_present(button_id) ⇒ Object



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

def assert_button_present(button_id)
  @web_browser.buttons.each { |button|
the_button_id = RWebSpec.framework == "Watir" ? button.id : button["id"]
    return if button_id == the_button_id
  }
  fail("can't find the button with id: #{button_id}")
end

#assert_button_present_with_text(button_text) ⇒ Object



349
350
351
352
353
354
# File 'lib/rwebspec-common/assert.rb', line 349

def assert_button_present_with_text(button_text)
  @web_browser.buttons.each { |button|      
    return if button_text == element_value(button)
  }
  fail("can't find the button with text: #{button_text}")
end

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

Checkbox



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rwebspec-common/assert.rb', line 115

def assert_checkbox_not_selected(checkbox_name)
  @web_browser.checkboxes.each { |checkbox|
the_element_name = element_name(checkbox)
    if (the_element_name == checkbox_name) then   
    if is_selenium_element?(checkbox)
        perform_assertion {  assert(!checkbox.selected?, "Checkbox #{checkbox_name} is checked unexpectly") }      
  else
        perform_assertion {  assert(!checkbox.set?, "Checkbox #{checkbox_name} is checked unexpectly") }
  end
    end
  }
end

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



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rwebspec-common/assert.rb', line 129

def assert_checkbox_selected(checkbox_name)
  @web_browser.checkboxes.each { |checkbox|
the_element_name = element_name(checkbox)
    if (the_element_name == checkbox_name) then
  if is_selenium_element?(checkbox)     
        perform_assertion { assert(checkbox.selected?, "Checkbox #{checkbox_name} not checked") }
  else
        perform_assertion { assert(checkbox.set?, "Checkbox #{checkbox_name} not checked") }         
  end
    end
  }
end

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



357
358
359
# File 'lib/rwebspec-common/assert.rb', line 357

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)


366
367
368
369
370
371
372
373
# File 'lib/rwebspec-common/assert.rb', line 366

def assert_exists(tag, element_id)
      if RWebSpec.framework == "Watir"
    perform_assertion { assert(eval("#{tag}(:id, '#{element_id.to_s}').exists?"), "Element '#{tag}' with id: '#{element_id}' not found") }   
      else
perform_assertion { assert( @web_browser.driver.find_element(:tag_name => tag, :id => element_id))}
      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")


410
411
412
413
414
415
416
# File 'lib/rwebspec-common/assert.rb', line 410

def assert_hidden(tag, element_id)
      if RWebSpec.framework =~ /selenium/i
    perform_assertion { assert(!eval("#{tag}(:id, '#{element_id.to_s}').displayed?"), "Element '#{tag}' with id: '#{element_id}' is visible") }
      else
    perform_assertion { assert(!eval("#{tag}(:id, '#{element_id.to_s}').visible?"), "Element '#{tag}' with id: '#{element_id}' is visible") }
      end
end


75
76
77
78
79
# File 'lib/rwebspec-common/assert.rb', line 75

def assert_link_not_present_with_exact(link_text)
  @web_browser.links.each { |link|
    perform_assertion { assert(link_text != link.text, "unexpected link (exact): #{link_text} found") }
  }
end


95
96
97
98
99
# File 'lib/rwebspec-common/assert.rb', line 95

def assert_link_not_present_with_text(link_text)
  @web_browser.links.each { |link|
    perform_assertion { assert(!link.text.include?(link_text), "unexpected link containing: #{link_text} found") }
  }
end

Assert a link with specified text (exact match) in the page

<a href="">Click Me</a>
assert_link_present_with_exact("Click Me") => true
assert_link_present_with_exact("Click") => false


68
69
70
71
72
73
# File 'lib/rwebspec-common/assert.rb', line 68

def assert_link_present_with_exact(link_text)
  @web_browser.links.each { |link|
    return if link_text == link.text
  }
  fail( "can't find the link with text: #{link_text}")
end

Assert a link containing specified text in the page

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


86
87
88
89
90
91
92
93
# File 'lib/rwebspec-common/assert.rb', line 86

def assert_link_present_with_text(link_text)

   @web_browser.links.each { |link|
     return if link.text.include?(link_text)
   }
   fail( "can't find the link containing text: #{link_text}")
  
end

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



20
21
22
# File 'lib/rwebspec-common/assert.rb', line 20

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?



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rwebspec-common/assert.rb', line 378

def assert_not_exists(tag, element_id)
  if RWebSpec.framework == "Watir"      
    perform_assertion {  assert_not(eval("#{tag}(:id, '#{element_id.to_s}').exists?"), "Unexpected element'#{tag}' + with id: '#{element_id}' found")}
  else
    perform_assertion { 
       begin
         @web_browser.driver.find_element(:tag_name => tag, :id => element_id)
         fail("the element #{tag}##{element_id} found")
       rescue =>e  
       end
    } 
  end
end

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



24
25
26
# File 'lib/rwebspec-common/assert.rb', line 24

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/rwebspec-common/assert.rb', line 230

def assert_option_equals(select_name, option_label)
  @web_browser.select_lists.each { |select|
the_element_name = element_name(select)
    next unless the_element_name == select_name

if RWebSpec.framework == "Watir"

     select.options.each do |option| # items in the list

       if (option.text == option_label) then
         perform_assertion { assert_equal(select.value, option.value, "Select #{select_name}'s value is not equal to expected option label: '#{option_label}'") }
       end
     end
  
else
  select.find_elements(:tag_name => "option" ).each do |option|
      if (option.text == option_label) then
      assert option.selected?
    end
  end
  
end
  }
end

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rwebspec-common/assert.rb', line 166

def assert_option_not_present(select_name, option_label)
  @web_browser.select_lists.each { |select|
the_element_name = element_name(select)
    next unless the_element_name == select_name
    
    if RWebSpec.framework =~ /watir/i
      select.options.each do |option| # items in the list

        perform_assertion {  assert(!(option.text == option_label), "unexpected select option: #{option_label} for #{select_name} found") }
      end
    else
      select.find_elements(:tag_name => "option" ).each do |option|
        fail("unexpected option label: #{option_label} found") if option.text == option_label
      end
    end
    
  }
end

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rwebspec-common/assert.rb', line 208

def assert_option_present(select_name, option_label)
  @web_browser.select_lists.each { |select|
the_element_name = element_name(select)
    next unless the_element_name == select_name

if RWebSpec.framework == "Watir"
     select.options.each do |option| # items in the list

       return if option.text == option_label
     end
else
  select.find_elements(:tag_name => "option" ).each do |option|
       return if option.text == option_label
  end      
end

  }
  fail("can't find the combo box: #{select_name} with value: #{option_label}")
end

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



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rwebspec-common/assert.rb', line 257

def assert_option_value_equals(select_name, option_value)
  @web_browser.select_lists.each { |select|
the_element_name = element_name(select)
    next unless the_element_name == select_name

if RWebSpec.framework == "Watir"
     perform_assertion {  assert_equal(select.value, option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'") }
else
     perform_assertion {  assert_equal(element_value(select), option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'") }          
end
  }
end

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

select



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rwebspec-common/assert.rb', line 146

def assert_option_value_not_present(select_name, option_value)
  @web_browser.select_lists.each { |select|
    the_element_name = element_name(select)
    next unless the_element_name == select_name
    
    if RWebSpec.framework =~ /watir/i        
      select.options.each do |option| # items in the list

        perform_assertion {  assert(!(option.value == option_value), "unexpected select option: #{option_value} for #{select_name} found") }
      end
    else
      select.find_elements(:tag_name => "option" ).each do |option|
        fail("unexpected option value: #{option_label} found") if option.value == option_value
      end          
    end
  }
end

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



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rwebspec-common/assert.rb', line 186

def assert_option_value_present(select_name, option_value)
  @web_browser.select_lists.each { |select|
the_element_name = element_name(select)
    next unless the_element_name  == select_name

if RWebSpec.framework == "Watir"
     select.options.each do |option| # items in the list

       return if option.value == option_value
     end          
else          
  select.find_elements(:tag_name => "option" ).each do |option|
    return if element_value(option) == option_value
  end
end

  }
  fail("can't find the combo box with value: #{option_value}")
end

#assert_radio_option_not_present(radio_group, radio_option) ⇒ Object

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



277
278
279
280
281
282
283
284
# File 'lib/rwebspec-common/assert.rb', line 277

def assert_radio_option_not_present(radio_group, radio_option)
  @web_browser.radios.each { |radio|
the_element_name = element_name(radio)          
    if (the_element_name == radio_group) then
      perform_assertion {  assert(!(radio_option == element_value(radio) ), "unexpected radio option: " + radio_option  + " found") }
    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



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rwebspec-common/assert.rb', line 310

def assert_radio_option_not_selected(radio_group, radio_option)
  @web_browser.radios.each { |radio|
the_element_name = element_name(radio)            
    if (the_element_name == radio_group and radio_option == element_value(radio) ) then 
  if is_selenium_element?(radio)
        perform_assertion {  assert(!radio.selected?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected") }
  else
        perform_assertion {  assert(!radio.set?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected") }
  end
    end
  }
end

#assert_radio_option_present(radio_group, radio_option) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/rwebspec-common/assert.rb', line 286

def assert_radio_option_present(radio_group, radio_option)
  @web_browser.radios.each { |radio|
the_element_name = element_name(radio)        
    return if (the_element_name == radio_group) and (radio_option == element_value(radio) )
  }
  fail("can't find the radio option : '#{radio_option}'")
end

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



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rwebspec-common/assert.rb', line 294

def assert_radio_option_selected(radio_group, radio_option)
  @web_browser.radios.each { |radio|
the_element_name = element_name(radio)          
    if (the_element_name == radio_group and radio_option == element_value(radio) ) then
  if is_selenium_element?(radio)
        perform_assertion { assert(radio.selected?, "Radio button #{radio_group}-[#{radio_option}] not checked") }
  else
        perform_assertion { assert(radio.set?, "Radio button #{radio_group}-[#{radio_option}] not checked") }          
  end  
    end
  }
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



464
465
466
467
468
469
470
471
# File 'lib/rwebspec-common/assert.rb', line 464

def assert_text_field_value(textfield_name, text)
      if RWebSpec.framework == "Watir"       
    perform_assertion { assert_equal(text, text_field(:name, textfield_name).value) }
      else
the_element = @web_browser.driver.find_element(:name, textfield_name)
    perform_assertion { assert_equal(text, element_value(the_element)) }
      end      
end

#assert_text_in_element(element_id, text) ⇒ Object

– Not tested




477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/rwebspec-common/assert.rb', line 477

def assert_text_in_element(element_id, text)
  elem = element_by_id(element_id)
  if RWebSpec.framework == "Watir"              
    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}") }
  else
    perform_assertion { 
      # this works in text field

      assert(element_value(elem).include?(text), "the text #{text} not found in element #{element_id}")
      # TODO

    }
  end

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


36
37
38
# File 'lib/rwebspec-common/assert.rb', line 36

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


42
43
44
# File 'lib/rwebspec-common/assert.rb', line 42

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


54
55
56
# File 'lib/rwebspec-common/assert.rb', line 54

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 = {}) ⇒ Object Also known as: assert_text_not_in_table



451
452
453
454
# File 'lib/rwebspec-common/assert.rb', line 451

def assert_text_not_present_in_table(table_id, text, options = {})
  options[:just_plain_text] ||= false
  perform_assertion { assert_not(the_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


48
49
50
# File 'lib/rwebspec-common/assert.rb', line 48

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 = {}) ⇒ 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


444
445
446
447
# File 'lib/rwebspec-common/assert.rb', line 444

def assert_text_present_in_table(table_id, text, options = {})
  options[:just_plain_text] ||= false
  perform_assertion { assert(the_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



28
29
30
# File 'lib/rwebspec-common/assert.rb', line 28

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")


399
400
401
402
403
404
405
# File 'lib/rwebspec-common/assert.rb', line 399

def assert_visible(tag, element_id)
      if RWebSpec.framework =~ /selenium/i
    perform_assertion { assert(eval("#{tag}(:id, '#{element_id.to_s}').displayed?"), "Element '#{tag}' with id: '#{element_id}' not visible") }
      else
 perform_assertion { assert(eval("#{tag}(:id, '#{element_id.to_s}').visible?"), "Element '#{tag}' with id: '#{element_id}' not visible") }
      end
end

#element_name(elem) ⇒ Object



105
106
107
# File 'lib/rwebspec-common/assert.rb', line 105

def element_name(elem)
  elem.class.name =~ /Selenium/ ? elem['name'] : elem.name       
end

#element_value(elem) ⇒ Object



109
110
111
# File 'lib/rwebspec-common/assert.rb', line 109

def element_value(elem)
  elem.class.name =~ /Selenium/ ? elem['value'] : elem.value       
end

#fail(message) ⇒ Object



16
17
18
# File 'lib/rwebspec-common/assert.rb', line 16

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

#is_selenium_element?(elem) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rwebspec-common/assert.rb', line 101

def is_selenium_element?(elem)
  elem.class.name =~ /Selenium/
end