module RWebSpec::Assert

Public Instance Methods

assert_button_not_present(button_id) click to toggle source

Button

# File lib/rwebspec-common/assert.rb, line 324
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 331
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 337
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 345
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_checked(checkbox_name)
assert_checkbox_not_checked(checkbox_name)
assert_checkbox_not_selected(checkbox_name) click to toggle source

Checkbox

# File lib/rwebspec-common/assert.rb, line 111
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
Also aliased as: assert_checkbox_not_checked
assert_checkbox_selected(checkbox_name) click to toggle source
# File lib/rwebspec-common/assert.rb, line 125
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
Also aliased as: assert_checkbox_checked
assert_element_exists(tag, element_id)
Alias for: assert_exists
assert_element_not_exists?(tag, element_id)
Alias for: assert_not_exists
assert_equals(expected, actual, msg=nil) click to toggle source
# File lib/rwebspec-common/assert.rb, line 353
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) click to toggle source

Check a HTML element exists or not Example:

assert_exists("label", "receipt_date")
assert_exists(:span, :receipt_date)
# File lib/rwebspec-common/assert.rb, line 362
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_exists?(tag, element_id)
Alias for: assert_exists
assert_hidden(tag, element_id) click to toggle source

Assert tag with element id is hidden?, example

assert_hidden(:div, "secret")
assert_hidden(:span, "secret_span")
# File lib/rwebspec-common/assert.rb, line 406
def assert_hidden(tag, element_id)
                    if RWebSpec.framework =~ /selenium/
    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
Also aliased as: assert_not_visible
assert_menu_label(select_name, option_label)
assert_menu_label_present(select_name, option_label)
assert_menu_value(select_name, option_value)
assert_menu_value_present(select_name, option_value)
assert_nil(actual, msg="") click to toggle source
# File lib/rwebspec-common/assert.rb, line 11
def assert_nil(actual, msg="")
  perform_assertion { assert(actual.nil?, msg) }
end
assert_not(condition, msg = "") click to toggle source
# File lib/rwebspec-common/assert.rb, line 7
def assert_not(condition, msg = "")
  perform_assertion { assert(!condition, msg) }
end
assert_not_exists(tag, element_id) click to toggle source
# File lib/rwebspec-common/assert.rb, line 374
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_exists?(tag, element_id)
Alias for: assert_not_exists
assert_not_nil(actual, msg="") click to toggle source
# File lib/rwebspec-common/assert.rb, line 15
def assert_not_nil(actual, msg="")
  perform_assertion { assert(!actual.nil?, msg) }
end
assert_not_visible(tag, element_id)
Alias for: assert_hidden
assert_option_equals(select_name, option_label) click to toggle source
# File lib/rwebspec-common/assert.rb, line 226
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 162
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/
      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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 204
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 253
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) click to toggle source

select

# File lib/rwebspec-common/assert.rb, line 142
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/        
      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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 182
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_button_checked(radio_group, radio_option)
assert_radio_button_not_checked(radio_group, radio_option)
assert_radio_option_checked(radio_group, radio_option)
assert_radio_option_not_checked(radio_group, radio_option)
assert_radio_option_not_present(radio_group, radio_option) click to toggle source

radio_group is the name field, radio options 'value' field

# File lib/rwebspec-common/assert.rb, line 273
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 306
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 282
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 290
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_select_label(select_name, option_label)
assert_select_label_not_present(select_name, option_label)
assert_select_label_present(select_name, option_label)
assert_select_value(select_name, option_value)
assert_select_value_not_present(select_name, option_value)
assert_select_value_present(select_name, option_value)
assert_text_field_value(textfield_name, text) click to toggle source

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

# File lib/rwebspec-common/assert.rb, line 460
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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 473
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) click to toggle source

Assert text present in page source (html)

assert_text_in_page_source("<b>iTest2</b>  Cool") # <b>iTest2</b>  Cool
# File lib/rwebspec-common/assert.rb, line 32
def assert_text_in_page_source(text)
  perform_assertion { assert((@web_browser.page_source.include? text), 'expected html: ' + text + ' not found') }
end
assert_text_in_table(table_id, text, options = {})
assert_text_not_in_page_source(text) click to toggle source

Assert text not present in page source (html)

assert_text_not_in_page_source("<b>iTest2</b>  Cool") # <b>iTest2</b>  Cool
# File lib/rwebspec-common/assert.rb, line 38
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_in_table(table_id, text, options = {})
assert_text_not_present(text) click to toggle source

Assert text not present in page source (html)

assert_text_not_present("iTest2 Cool") # <b>iTest2</b>  Cool
# File lib/rwebspec-common/assert.rb, line 50
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 = {}) click to toggle source
# File lib/rwebspec-common/assert.rb, line 447
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
Also aliased as: assert_text_not_in_table
assert_text_present(text) click to toggle source

Assert text present in page source (html)

assert_text_present("iTest2 Cool") # <b>iTest2</b>  Cool
# File lib/rwebspec-common/assert.rb, line 44
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 = {}) click to toggle source

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
# File lib/rwebspec-common/assert.rb, line 440
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
Also aliased as: assert_text_in_table
assert_title(title)
Alias for: assert_title_equals
assert_title_equals(title) click to toggle source

assertions

# File lib/rwebspec-common/assert.rb, line 24
def assert_title_equals(title)
  assert_equals(title, @web_browser.page_title)
end
Also aliased as: assert_title
assert_visible(tag, element_id) click to toggle source

Assert tag with element id is visible?, eg.

assert_visible(:div, "public_notice")
assert_visible(:span, "public_span")
# File lib/rwebspec-common/assert.rb, line 395
def assert_visible(tag, element_id)
                    if RWebSpec.framework =~ /selenium/
    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) click to toggle source
# File lib/rwebspec-common/assert.rb, line 101
def element_name(elem)
        elem.class.name =~ /Selenium/ ? elem['name'] : elem.name                     
end
element_value(elem) click to toggle source
# File lib/rwebspec-common/assert.rb, line 105
def element_value(elem)
        elem.class.name =~ /Selenium/ ? elem['value'] : elem.value                   
end
fail(message) click to toggle source
# File lib/rwebspec-common/assert.rb, line 19
def fail(message)
  perform_assertion { assert(false, message) }
end
is_selenium_element?(elem) click to toggle source
# File lib/rwebspec-common/assert.rb, line 97
def is_selenium_element?(elem)
        elem.class.name =~ /Selenium/
end