Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement "path" method for Node

  • Loading branch information...
commit 4ceb8740faa453740a958d5c38ac56361b55bb5b 1 parent 7fe06e9
@niklasb niklasb authored halogenandtoast committed
Showing with 38 additions and 1 deletion.
  1. +1 −1  lib/capybara/driver/webkit/node.rb
  2. +37 −0 src/capybara.js
View
2  lib/capybara/driver/webkit/node.rb
@@ -74,7 +74,7 @@ def disabled?
end
def path
- raise Capybara::NotSupportedByDriverError
+ invoke "path"
end
def trigger(event)
View
37 src/capybara.js
@@ -56,6 +56,43 @@ Capybara = {
}
},
+ path: function(index) {
+ return "/" + this.getXPathNode(this.nodes[index]).join("/");
+ },
+
+ getXPathNode: function(node, path) {
+ path = path || [];
+ if(node.parentNode) {
+ path = this.getXPathNode(node.parentNode, path);
+ }
+
+ if(node.previousSibling) {
+ var count = 1;
+ var sibling = node.previousSibling
+ do {
+ if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
+ sibling = sibling.previousSibling;
+ } while(sibling);
+ if(count == 1) {count = null;}
+ } else if(node.nextSibling) {
+ var sibling = node.nextSibling;
+ do {
+ if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
+ var count = 1;
+ sibling = null;
+ } else {
+ var count = null;
+ sibling = sibling.previousSibling;
+ }
+ } while(sibling);
+ }
+
+ if(node.nodeType == 1) {
+ path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
+ }
+ return path;
+ },
+
tagName: function(index) {
return this.nodes[index].tagName.toLowerCase();
},
Please sign in to comment.
Something went wrong with that request. Please try again.