Dashboard > Watir > ... > Questionable Examples > Assertion (Verify) to Compare Arrays
Watir Log In View a printable version of the current page.
Assertion (Verify) to Compare Arrays
Added by Dara Lillis, last edited by Alister Scott on Sep 29, 2008  (view change)
Labels: 
(None)

The following method compares two arrays and gives a user-friendly message on any mismatched elements. I patch this on to Watir in a patch file, in a module called Assertions inside a module called Watir (I require this patch file when running my tests).

#compare two arrays, fail if they don't match
#report the unmatched elements in a readable format
def verify_array(expected_array, actual_array, message = '')
  #array1 - array2 gives an array containing only the differences
  #(elements in array1 not found in array2)
  differences = (expected_array - actual_array)
  #for each difference, find the index of the value that doesn't match, 
  #so you can show both expected and actual
  differences.each do |difference|
    difference_index = expected_array.index(difference)
    failure = "\n#{"="*30}\nDifference at index #{difference_index.to_s}.\n\#line continues
Expected array value: #{difference.to_s}\nActual array value: #{actual_array[difference_index].to_s}"
    message = message + "\n#{failure}"
  end
  verify(expected_array == actual_array, message)
end

This isn't specifically to do with Watir.

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators