From a138207e94ac3dde423c0c8809017716f9e5e3b3 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Thu, 5 Nov 2009 00:34:11 +0100 Subject: [PATCH] Finding nodes via XPath --- lib/webcat/culerity_driver.rb | 19 +++++++++++++++++- lib/webcat/rack_test_driver.rb | 22 +++++++++++++++++++++ spec/drivers_spec.rb | 35 +++++++++++++++++++++++++++++----- spec/test_app.rb | 4 ++++ spec/views/with_html.erb | 15 +++++++++++++++ 5 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 spec/views/with_html.erb diff --git a/lib/webcat/culerity_driver.rb b/lib/webcat/culerity_driver.rb index a7401511..644112aa 100644 --- a/lib/webcat/culerity_driver.rb +++ b/lib/webcat/culerity_driver.rb @@ -3,7 +3,20 @@ require 'rack' require 'net/http' class Webcat::Driver::Culerity - Response = Struct.new(:body) + class Node < Struct.new(:node) + def text + node.text + end + + def attribute(name) + value = if name.to_sym == :class + node.class_name + else + node.send(name.to_sym) + end + return value if value and not value.empty? + end + end attr_reader :app, :rack_server @@ -30,6 +43,10 @@ class Webcat::Driver::Culerity def body browser.html end + + def find(selector) + browser.elements_by_xpath(selector).map { |node| Node.new(node) } + end private diff --git a/lib/webcat/rack_test_driver.rb b/lib/webcat/rack_test_driver.rb index 58ac2921..5376b428 100644 --- a/lib/webcat/rack_test_driver.rb +++ b/lib/webcat/rack_test_driver.rb @@ -1,6 +1,18 @@ require 'rack/test' +require 'nokogiri' class Webcat::Driver::RackTest + class Node < Struct.new(:node) + def text + node.text + end + + def attribute(name) + value = node.attributes[name.to_s] + return value.to_s if value + end + end + include ::Rack::Test::Methods attr_reader :app @@ -18,4 +30,14 @@ class Webcat::Driver::RackTest def body response.body end + + def find(selector) + html.xpath(selector).map { |node| Node.new(node) } + end + +private + + def html + Nokogiri::HTML(body) + end end \ No newline at end of file diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 4ce4194d..8407ff1b 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -1,7 +1,7 @@ require File.expand_path('spec_helper', File.dirname(__FILE__)) shared_examples_for 'driver' do - + describe '#visit' do it "should move to another page" do @driver.visit('/') @@ -10,17 +10,42 @@ shared_examples_for 'driver' do @driver.body.should == 'Another World' end end - + describe '#body' do it "should return text reponses" do @driver.visit('/') @driver.body.should == 'Hello world!' end - + it "should return the full response html" do @driver.visit('/with_simple_html') @driver.body.should == '

Bar

' end end - -end \ No newline at end of file + + describe '#find' do + context "with xpath selector" do + before do + @driver.visit('/with_html') + end + + it "should find the correct number of elements" do + @driver.find('//a').size.should == 2 + end + + it "should extract node texts" do + @driver.find('//a')[0].text.should == 'labore' + @driver.find('//a')[1].text.should == 'ullamco' + end + + it "should extract node attributes" do + @driver.find('//a')[0].attribute(:href).should == '/with_simple_html' + @driver.find('//a')[0].attribute(:class).should == 'simple' + @driver.find('//a')[1].attribute(:href).should == '/foo' + @driver.find('//a')[1].attribute(:id).should == 'foo' + @driver.find('//a')[1].attribute(:rel).should be_nil + end + end + end + +end diff --git a/spec/test_app.rb b/spec/test_app.rb index 97ecace0..bfdcde2a 100644 --- a/spec/test_app.rb +++ b/spec/test_app.rb @@ -8,6 +8,10 @@ class TestApp < Sinatra::Base get '/foo' do 'Another World' end + + get '/with_html' do + erb :with_html + end get '/with_simple_html' do erb :with_simple_html diff --git a/spec/views/with_html.erb b/spec/views/with_html.erb new file mode 100644 index 00000000..70f23d13 --- /dev/null +++ b/spec/views/with_html.erb @@ -0,0 +1,15 @@ +

This is a test

+ +

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore + et dolore magna aliqua. Ut enim ad minim veniam, + quis nostrud exercitation ullamco laboris nisi + ut aliquip ex ea commodo consequat. +

+ +

+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum + dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, + sunt in culpa qui officia deserunt mollit anim id est laborum. +

\ No newline at end of file