Skip to content

Instantly share code, notes, and snippets.

@ngty
Created February 26, 2009 15:13
Show Gist options
  • Save ngty/70900 to your computer and use it in GitHub Desktop.
Save ngty/70900 to your computer and use it in GitHub Desktop.
###
# Generic step that supports traversing a html table to find specific row identified by i_bale/i_value
# pair, and to test whether value described by of t_label matches the expected t_value.
#
# Eg.
# <div class="annual_leave">
# <table>
# <tr>
# <td>
# <div>
# <label>i_label</label>
# <span>i_value</span>
# </div>
# ...
# <div>
# <label>t_label</label>
# <span>t_value</span>
# </div>
# </td>
# </tr>
# ...
# </table>
# </div>
#
Then /^I should see (\w+) with "([^"]+)" as "([^"]+)" showing "([^"]+)" as "([^"]+)"$/ do
| resource, i_label, i_value, t_label, t_value |
get_text(locate_one(%w{
//div[@class="%s"]//label[normalize-space(text())="%s"]
following-sibling::span[normalize-space(text())="%s"]
ancestor::tr//label[normalize-space(text())="%s"]
following-sibling::span
}.join("/") % [ table_of(resource), i_label, i_value, t_label ]
).locator).should == t_value
end
def table_of(resource)
resource.to_s.sub(/\s+/,'_').singularize.tableize
end
###
# Adding selenium support to cucumber test suite
#
require 'rubygems'
gem 'rspec', '>=1.1.12'
gem 'selenium-client', '>=1.2.10'
gem 'nokogiri', '>=1.2.1'
require 'selenium/client'
require 'nokogiri'
module Merb
module Test
module World
module Response
def response
Nokogiri::Hpricot(get_html_source)
end
def locate_one(path)
locate_single(path)
end
def locate_many(*paths)
paths = [paths].flatten
locate_multiple(*paths)
end
private
def locate_single(path)
if path.include?(' | ')
locate_multiple(*path.split(' | '))[0]
else
(node=response.at(path)) ? add_locator!(node) : nil
end
end
def locate_multiple(*paths)
unless (nodes=response.search(*paths)).empty?
nodes.map {|n| add_locator!(n) }
else
[]
end
end
def add_locator!(node)
unless node.respond_to?(:locator)
class << node
def locator
path.sub(/^\/html\/body/,'/')
end
end
end
node
end
end
class Selenium
include ::Selenium::Client::Base
include Response
def start_browser(browser_string)
@browser_string = browser_string
start_new_browser_session
end
def close_browser
close_current_browser_session
end
end
end
end
end
World do
Merb::Test::World::Selenium.new( 'localhost', 4444, '*', 'http://localhost:4000', 300 )
end
###
# Matches an annual leave step like the following in a cucumber feature file:
# "
# Then I should see annual leave "2009-02-20 (afternoon)" with "Status" as "Approved"
# "
#
Then /^I should see annual leave "([^"]+)" with "([^"]+)" as "([^"]+)"$/ do | date_time, label, value |
Then 'I should see annual_leave "%s" with "%s" as "%s" showing "%s" as "%s"' %
[ 'Date/Time', date_time, label, value ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment