Finding nodes via XPath
This commit is contained in:
parent
c4ee7eaa69
commit
a138207e94
|
@ -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
|
||||
|
||||
|
@ -31,6 +44,10 @@ class Webcat::Driver::Culerity
|
|||
browser.html
|
||||
end
|
||||
|
||||
def find(selector)
|
||||
browser.elements_by_xpath(selector).map { |node| Node.new(node) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url(path)
|
||||
|
|
|
@ -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
|
|
@ -23,4 +23,29 @@ shared_examples_for 'driver' do
|
|||
end
|
||||
end
|
||||
|
||||
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
|
|
@ -9,6 +9,10 @@ class TestApp < Sinatra::Base
|
|||
'Another World'
|
||||
end
|
||||
|
||||
get '/with_html' do
|
||||
erb :with_html
|
||||
end
|
||||
|
||||
get '/with_simple_html' do
|
||||
erb :with_simple_html
|
||||
end
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<h1>This is a test</h1>
|
||||
|
||||
<p>
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
|
||||
tempor incididunt ut <a href="/with_simple_html" class="simple">labore</a>
|
||||
et dolore magna aliqua. Ut enim ad minim veniam,
|
||||
quis nostrud exercitation <a href="/foo" id="foo">ullamco</a> laboris nisi
|
||||
ut aliquip ex ea commodo consequat.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
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.
|
||||
</p>
|
Loading…
Reference in New Issue