Implemented Node#find

This commit is contained in:
Joe Ferris 2011-02-26 16:46:59 -05:00
parent 9c28c0096d
commit 69ef4e73dc
3 changed files with 38 additions and 2 deletions

View File

@ -50,6 +50,12 @@ class Capybara::Driver::Webkit
invoke "trigger", event
end
def find(xpath)
invoke("findWithin", xpath).split(',').map do |native|
self.class.new(driver, native)
end
end
def invoke(name, *args)
browser.command "Node", name, native, *args
end

View File

@ -242,4 +242,27 @@ describe Capybara::Driver::Webkit do
subject.find("//*[@class='triggered']").size.should == 2
end
end
context "nesting app" do
let(:app) do
lambda do |env|
body = <<-HTML
<html><body>
<div id="parent">
<div class="find">Expected</div>
</div>
<div class="find">Unexpected</div>
</body></html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end
it "evaluates nested xpath expressions" do
parent = subject.find("//*[@id='parent']").first
parent.find("./*[@class='find']").map(&:text).should == %w(Expected)
end
end
end

View File

@ -7,7 +7,15 @@ Capybara = {
},
find: function (xpath) {
var iterator = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
return this.findRelativeTo(document, xpath);
},
findWithin: function (index, xpath) {
return this.findRelativeTo(this.nodes[index], xpath);
},
findRelativeTo: function (reference, xpath) {
var iterator = document.evaluate(xpath, reference, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node;
var results = [];
while (node = iterator.iterateNext()) {
@ -59,6 +67,5 @@ Capybara = {
set: function(index, value) {
this.nodes[index].value = value;
}
};