module RWebSpec::Assert
Public Instance Methods
own assert method
# 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
Checkbox
# 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
# 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
# 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
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 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
# 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
# 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
# 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 ") # =>
# 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
# File lib/rwebspec-common/assert.rb, line 20 def assert_not(condition, msg = "") perform_assertion { assert(!condition, msg) } end
# 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
# File lib/rwebspec-common/assert.rb, line 24 def assert_not_nil(actual, msg="") perform_assertion { assert(!actual.nil?, msg) } end
# 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
# 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
# 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
# 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
select
# 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
# 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
radio_group is the name field, radio options 'value' field
# 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
# 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
# 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
# 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
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 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
# 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 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 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 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 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 in page source (html)
assert_text_not_present("iTest2 Cool") # <b>iTest2</b> Cool
# 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
# 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 in page source (html)
assert_text_present("iTest2 Cool") # <b>iTest2</b> Cool
# 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
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 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
# File lib/rwebspec-common/assert.rb, line 28 def assert_title_equals(title) assert_equals(title, @web_browser.page_title) end
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 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
# File lib/rwebspec-common/assert.rb, line 105 def element_name(elem) elem.class.name =~ /Selenium/ ? elem['name'] : elem.name end
# File lib/rwebspec-common/assert.rb, line 109 def element_value(elem) elem.class.name =~ /Selenium/ ? elem['value'] : elem.value end
# File lib/rwebspec-common/assert.rb, line 16 def fail(message) perform_assertion { assert(false, message) } end
# File lib/rwebspec-common/assert.rb, line 101 def is_selenium_element?(elem) elem.class.name =~ /Selenium/ end
Private Instance Methods
# File lib/rwebspec-common/assert.rb, line 509 def perform_assertion(&block) begin yield rescue StandardError => e # puts "[DEBUG] Assertion error: #{e}" take_screenshot if $take_screenshot raise e rescue RWebSpec::Assertion => e2 take_screenshot if $take_screenshot raise e2 end end
TODO for drag-n-drop, check the postion in list
def assert_position_in_list(list_element_id) raise "not implemented" end
# File lib/rwebspec-common/assert.rb, line 501 def the_table_source(table_id, options) elem_table = RWebSpec.framework == "Watir" ? table(:id, table_id.to_s) : @web_browser.driver.find_element(:id => table_id) elem_table_text = elem_table.text elem_table_html = RWebSpec.framework == "Watir" ? elem_table.html : elem_table["innerHTML"]; table_source = options[:just_plain_text] ? elem_table_text : elem_table_html end