Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
implement "path" method for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasb authored and halogenandtoast committed Jan 13, 2012
1 parent 7fe06e9 commit 4ceb874
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/capybara/driver/webkit/node.rb
Expand Up @@ -74,7 +74,7 @@ def disabled?
end

def path
raise Capybara::NotSupportedByDriverError
invoke "path"
end

def trigger(event)
Expand Down
37 changes: 37 additions & 0 deletions src/capybara.js
Expand Up @@ -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();
},
Expand Down

0 comments on commit 4ceb874

Please sign in to comment.