diff --git a/lib/capybara/driver/webkit/node.rb b/lib/capybara/driver/webkit/node.rb
index 9bcf19f..4d3460c 100644
--- a/lib/capybara/driver/webkit/node.rb
+++ b/lib/capybara/driver/webkit/node.rb
@@ -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
diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb
index 9380a5b..8a7a5b6 100644
--- a/spec/driver_spec.rb
+++ b/spec/driver_spec.rb
@@ -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
+
+
+ Unexpected
+
+ 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
diff --git a/src/capybara.js b/src/capybara.js
index 46af359..ed689c5 100644
--- a/src/capybara.js
+++ b/src/capybara.js
@@ -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;
}
-
};