mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
Implemented Node#find
This commit is contained in:
parent
9c28c0096d
commit
69ef4e73dc
3 changed files with 38 additions and 2 deletions
|
@ -50,6 +50,12 @@ class Capybara::Driver::Webkit
|
||||||
invoke "trigger", event
|
invoke "trigger", event
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find(xpath)
|
||||||
|
invoke("findWithin", xpath).split(',').map do |native|
|
||||||
|
self.class.new(driver, native)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def invoke(name, *args)
|
def invoke(name, *args)
|
||||||
browser.command "Node", name, native, *args
|
browser.command "Node", name, native, *args
|
||||||
end
|
end
|
||||||
|
|
|
@ -242,4 +242,27 @@ describe Capybara::Driver::Webkit do
|
||||||
subject.find("//*[@class='triggered']").size.should == 2
|
subject.find("//*[@class='triggered']").size.should == 2
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -7,7 +7,15 @@ Capybara = {
|
||||||
},
|
},
|
||||||
|
|
||||||
find: function (xpath) {
|
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 node;
|
||||||
var results = [];
|
var results = [];
|
||||||
while (node = iterator.iterateNext()) {
|
while (node = iterator.iterateNext()) {
|
||||||
|
@ -59,6 +67,5 @@ Capybara = {
|
||||||
set: function(index, value) {
|
set: function(index, value) {
|
||||||
this.nodes[index].value = value;
|
this.nodes[index].value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue