Watir-Webdriver Sort Test

Jan 24, 2013

watir.logoWhile convalescing this past weekend from some kinda cold/funk that's been making the rounds, I spent some time playing with Watir-Webdriver. I really like Watir and have heard good things about Webdriver, so I wanted to see for myself. As an exercise, I decided to port my Sahi sort test from a previous post... here's what I came up with.

\# ~ sort test...

require 'rubygems'
require 'watir-webdriver'

browser = Watir::Browser.start 'http://www.javascriptkit.com/script/script2/sorttable.shtml', :chrome

# initial sort...
browser.link(:text => 'Name').click

# collect table column elements in an array...
app\_sorted\_values = Array.new
# itterate through each row (skip the 1st)...
browser.table(:id, 'table0').rows.each.drop(1).each do |row|
    app\_sorted\_values << row.cell.text
 end

# sort with ruby case-insensitive...
ruby\_sorted\_values = app\_sorted\_values.sort {|a,b| a.upcase <=> b.upcase}

puts "App sort: #{app\_sorted\_values} \\nRuby sort: #{ruby\_sorted\_values}"

browser.close

Do feel free to comment and/or make suggestions!