Skip to content
Browse files

added specs for Node#path and adapted Javascript code to pass

  • Loading branch information...
1 parent 83905bb commit 21f4b84691d00019e158c5256dda69f4b89ff338 @niklasb niklasb committed with halogenandtoast
Showing with 84 additions and 23 deletions.
  1. +66 −0 spec/driver_spec.rb
  2. +18 −23 src/capybara.js
View
66 spec/driver_spec.rb
@@ -1063,6 +1063,72 @@ def set_automatic_reload(value)
end
end
+ context "app with a lot of HTML tags" do
+ before(:all) do
+ @app = lambda do |env|
+ body = <<-HTML
+ <html>
+ <head>
+ <title>My eBook</title>
+ <meta class="charset" name="charset" value="utf-8" />
+ <meta class="author" name="author" value="Firstname Lastname" />
+ </head>
+ <body>
+ <div id="toc">
+ <table>
+ <thead id="head">
+ <tr><td class="td1">Chapter</td><td>Page</td></tr>
+ </thead>
+ <tbody>
+ <tr><td>Intro</td><td>1</td></tr>
+ <tr><td>Chapter 1</td><td class="td2">1</td></tr>
+ <tr><td>Chapter 2</td><td>1</td></tr>
+ </tbody>
+ </table>
+ </div>
+
+ <h1 class="h1">My first book</h1>
+ <p class="p1">Written by me</p>
+ <div id="intro" class="intro">
+ <p>Let's try out XPath</p>
+ <p class="p2">in capybara-webkit</p>
+ </div>
+
+ <h2 class="chapter1">Chapter 1</h2>
+ <p>This paragraph is fascinating.</p>
+ <p class="p3">But not as much as this one.</p>
+
+ <h2 class="chapter2">Chapter 2</h2>
+ <p>Let's try if we can select this</p>
+ </body>
+ </html>
+ HTML
+ [200,
+ { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
+ [body]]
+ end
+ end
+
+ it "builds up node paths correctly" do
+ cases = {
+ "//*[contains(@class, 'author')]" => "/html/head/meta[2]",
+ "//*[contains(@class, 'td1')]" => "/html/body/div[@id='toc']/table/thead[@id='head']/tr/td[1]",
+ "//*[contains(@class, 'td2')]" => "/html/body/div[@id='toc']/table/tbody/tr[2]/td[2]",
+ "//h1" => "/html/body/h1",
+ "//*[contains(@class, 'chapter2')]" => "/html/body/h2[2]",
+ "//*[contains(@class, 'p1')]" => "/html/body/p[1]",
+ "//*[contains(@class, 'p2')]" => "/html/body/div[@id='intro']/p[2]",
+ "//*[contains(@class, 'p3')]" => "/html/body/p[3]",
+ }
+
+ cases.each do |xpath, path|
+ nodes = subject.find(xpath)
+ nodes.size.should == 1
+ nodes[0].path.should == path
+ end
+ end
+ end
+
context "css overflow app" do
before(:all) do
@app = lambda do |env|
View
41 src/capybara.js
@@ -62,34 +62,29 @@ Capybara = {
getXPathNode: function(node, path) {
path = path || [];
- if(node.parentNode) {
+ 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);
+ var first = node;
+ while (first.previousSibling)
+ first = first.previousSibling;
+
+ var count = 0;
+ var index = 0;
+ var iter = first;
+ while (iter) {
+ if (iter.nodeType == 1 && iter.nodeName == node.nodeName)
+ count++;
+ if (iter.isSameNode(node))
+ index = count;
+ iter = iter.nextSibling;
+ continue;
}
- if(node.nodeType == 1) {
- path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
- }
+ if (node.nodeType == 1)
+ path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 1 ? "["+index+"]" : ''));
+
return path;
},

0 comments on commit 21f4b84

Please sign in to comment.
Something went wrong with that request. Please try again.