Skip to content

Commit

Permalink
Moved over tableish from cucumber
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Dec 7, 2009
1 parent 5b79b22 commit 2a12bb4
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 2 deletions.
2 changes: 2 additions & 0 deletions History.txt
@@ -1,8 +1,10 @@
== In Git

=== New features
* New #tableish method to extract table-like data from a HTML page. Pure Nokogiri/CSS3/XPath. (Aslak Hellesøy)

=== Bugfixes
* Make #element_at / #table_at work on Webrat 0.6.0. This is now deprecated it in favour of #tableish. (Aslak Hellesøy)
* Webrat step "Then I should be on" should use request_uri instead of path for missing query string parameters (ZhangJinzhu)
* Added MIME type parameter to attach file step definition (Felix Flores)
* Added check when including ActiveSupport::Testing::SetupAndTeardown for older Rails versions (Jeremy Durham)
4 changes: 3 additions & 1 deletion generators/cucumber/templates/webrat_env.rb
@@ -1,4 +1,6 @@
require 'cucumber/webrat/element_locator' # Lets you do table.diff!(element_at('#my_table_or_dl_or_ul_or_ol').to_table)
require 'cucumber/webrat/element_locator' # Deprecated - remove this line if you don't use #element_at or #table_at
require 'cucumber/web/tableish'
World(Cucumber::Tableish)
require 'webrat'
require 'webrat/core/matchers'
Webrat.configure do |config|
Expand Down
2 changes: 1 addition & 1 deletion generators/feature/templates/steps.erb
Expand Up @@ -10,5 +10,5 @@ When /^I delete the (\d+)(?:st|nd|rd|th) <%= singular_name %>$/ do |pos|
end

Then /^I should see the following <%= plural_name %>:$/ do |expected_<%= plural_name %>_table|
expected_<%= plural_name %>_table.diff!(table_at('table').to_a)
expected_<%= plural_name %>_table.diff!(tableish('table tr', 'td,th'))
end
26 changes: 26 additions & 0 deletions lib/cucumber/web/tableish.rb
@@ -0,0 +1,26 @@
require 'nokogiri'

module Cucumber
module Tableish
def tableish(row_selector, column_selectors)
_tableish(response_body, row_selector, column_selectors)
end

def _tableish(html, row_selector, column_selectors)
doc = Nokogiri::HTML(html)
column_count = nil
doc.search(row_selector).map do |row|
cells = case(column_selectors)
when String
row.search(column_selectors)
when Proc
column_selectors.call(row)
end
column_count ||= cells.length
cells[0...column_count].map do |cell|
cell.text.strip
end
end
end
end
end
103 changes: 103 additions & 0 deletions spec/cucumber/web/tableish_spec.rb
@@ -0,0 +1,103 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
require 'cucumber/web/tableish'

module Cucumber
module Web
describe Tableish do
include Tableish

unless RUBY_PLATFORM =~ /java/
it "should convert a table" do
html = <<-HTML
<table>
<tr>
<th>tool</th>
<th>dude</th>
</tr>
<tr>
<td>webrat</td>
<td>bryan</td>
</tr>
<tr>
<td>cucumber</td>
<td>aslak</td>
</tr>
</table>
HTML

_tableish(html, 'table tr', 'td,th').should == [
%w{tool dude},
%w{webrat bryan},
%w{cucumber aslak}
]
end

it "should size to the first row" do
html = <<-HTML
<table>
<tr>
<th>tool</th>
<th>dude</th>
</tr>
<tr>
<td>webrat</td>
<td>bryan</td>
<td>crapola</td>
</tr>
<tr>
<td>cucumber</td>
<td>aslak</td>
<td>gunk</td>
<td>filth</td>
</tr>
</table>
HTML

_tableish(html, 'table tr', 'td,th').should == [
%w{tool dude},
%w{webrat bryan},
%w{cucumber aslak}
]
end

it "should convert a dl" do
html = <<-HTML
<dl>
<dt>webrat</dt>
<dd>bryan</dd>
<dt>cucumber</dt>
<dd>aslak</dd>
</dl>
HTML

_tableish(html, 'dl dt', lambda{|dt| [dt, dt.next.next]}).should == [
%w{webrat bryan},
%w{cucumber aslak}
]
end

it "should convert a ul" do
html = <<-HTML
<ul id="phony">
<li>nope</li>
</ul>
<ul id="yes">
<li>webrat</li>
<li>bryan</li>
<li>cucumber</li>
<li>aslak</li>
</ul>
HTML

_tableish(html, 'ul#yes li', lambda{|li| [li]}).should == [
%w{webrat},
%w{bryan},
%w{cucumber},
%w{aslak},
]
end
end
end
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,6 @@
require 'rubygems'
gem 'rspec'
require 'spec'
require 'spec/autorun'

$:.unshift(File.dirname(__FILE__) + '/../lib')
13 changes: 13 additions & 0 deletions tasks/rspec.rake
@@ -0,0 +1,13 @@
begin
require 'spec/rake/spectask'

desc "Run RSpec"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = %w{--color --diff --format profile}
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = ENV['RCOV']
t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/}
end
rescue LoadError
task :spec
end

0 comments on commit 2a12bb4

Please sign in to comment.