diff --git a/lib/capybara/poltergeist/browser.rb b/lib/capybara/poltergeist/browser.rb index 6d51cf9..fd3a6da 100644 --- a/lib/capybara/poltergeist/browser.rb +++ b/lib/capybara/poltergeist/browser.rb @@ -43,8 +43,8 @@ module Capybara::Poltergeist result['ids'].map { |id| [result['page_id'], id] } end - def find_within(page_id, id, selector) - command 'find_within', page_id, id, selector + def find_within(page_id, id, method, selector) + command 'find_within', page_id, id, method, selector end def text(page_id, id) diff --git a/lib/capybara/poltergeist/client/agent.coffee b/lib/capybara/poltergeist/client/agent.coffee index 33c30e1..b512057 100644 --- a/lib/capybara/poltergeist/client/agent.coffee +++ b/lib/capybara/poltergeist/client/agent.coffee @@ -28,13 +28,13 @@ class PoltergeistAgent window.location.toString() find: (method, selector, within = document) -> - results = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) - ids = [] + if method == "xpath" + xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) + results = (xpath.snapshotItem(i) for i in [0...xpath.snapshotLength]) + else + results = within.querySelectorAll(selector) - for i in [0...results.snapshotLength] - ids.push(this.register(results.snapshotItem(i))) - - ids + this.register(el) for el in results register: (element) -> @elements.push(element) @@ -73,8 +73,8 @@ class PoltergeistAgent.Node parentId: -> @agent.register(@element.parentNode) - find: (selector) -> - @agent.find(selector, @element) + find: (method, selector) -> + @agent.find(method, selector, @element) isObsolete: -> obsolete = (element) => diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index 5c03f2a..d855c06 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -101,8 +101,8 @@ class Poltergeist.Browser find: (method, selector) -> this.sendResponse(page_id: @page_id, ids: @page.find(method, selector)) - find_within: (page_id, id, selector) -> - this.sendResponse this.node(page_id, id).find(selector) + find_within: (page_id, id, method, selector) -> + this.sendResponse this.node(page_id, id).find(method, selector) text: (page_id, id) -> this.sendResponse this.node(page_id, id).text() diff --git a/lib/capybara/poltergeist/client/compiled/agent.js b/lib/capybara/poltergeist/client/compiled/agent.js index d1565e8..0308e65 100644 --- a/lib/capybara/poltergeist/client/compiled/agent.js +++ b/lib/capybara/poltergeist/client/compiled/agent.js @@ -45,16 +45,29 @@ PoltergeistAgent = (function() { }; PoltergeistAgent.prototype.find = function(method, selector, within) { - var i, ids, results, _i, _ref; + var el, i, results, xpath, _i, _len, _results; if (within == null) { within = document; } - results = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); - ids = []; - for (i = _i = 0, _ref = results.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - ids.push(this.register(results.snapshotItem(i))); + if (method === "xpath") { + xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + results = (function() { + var _i, _ref, _results; + _results = []; + for (i = _i = 0, _ref = xpath.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + _results.push(xpath.snapshotItem(i)); + } + return _results; + })(); + } else { + results = within.querySelectorAll(selector); } - return ids; + _results = []; + for (_i = 0, _len = results.length; _i < _len; _i++) { + el = results[_i]; + _results.push(this.register(el)); + } + return _results; }; PoltergeistAgent.prototype.register = function(element) { @@ -123,8 +136,8 @@ PoltergeistAgent.Node = (function() { return this.agent.register(this.element.parentNode); }; - Node.prototype.find = function(selector) { - return this.agent.find(selector, this.element); + Node.prototype.find = function(method, selector) { + return this.agent.find(method, selector, this.element); }; Node.prototype.isObsolete = function() { diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index 536e6de..cc5d1f2 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -131,8 +131,8 @@ Poltergeist.Browser = (function() { }); }; - Browser.prototype.find_within = function(page_id, id, selector) { - return this.sendResponse(this.node(page_id, id).find(selector)); + Browser.prototype.find_within = function(page_id, id, method, selector) { + return this.sendResponse(this.node(page_id, id).find(method, selector)); }; Browser.prototype.text = function(page_id, id) { diff --git a/lib/capybara/poltergeist/driver.rb b/lib/capybara/poltergeist/driver.rb index 0ea9fc1..84408e8 100644 --- a/lib/capybara/poltergeist/driver.rb +++ b/lib/capybara/poltergeist/driver.rb @@ -114,6 +114,10 @@ module Capybara::Poltergeist find :xpath, selector end + def find_css(selector) + find :css, selector + end + def click(x, y) browser.click_coordinates(x, y) end @@ -128,6 +132,7 @@ module Capybara::Poltergeist end def within_frame(name, &block) + raise NotImplementedError browser.within_frame(name, &block) end diff --git a/lib/capybara/poltergeist/node.rb b/lib/capybara/poltergeist/node.rb index 4ec99d3..b303c7f 100644 --- a/lib/capybara/poltergeist/node.rb +++ b/lib/capybara/poltergeist/node.rb @@ -29,8 +29,16 @@ module Capybara::Poltergeist end end - def find(selector) - command(:find_within, selector).map { |id| self.class.new(driver, page_id, id) } + def find(method, selector) + command(:find_within, method, selector).map { |id| self.class.new(driver, page_id, id) } + end + + def find_xpath(selector) + find :xpath, selector + end + + def find_css(selector) + find :css, selector end def text