From 9add4cc83dc03da17b7d92520d4494929ff57efe Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 3 Mar 2013 14:21:12 +0000 Subject: [PATCH] Implement all_text and visible_text --- lib/capybara/poltergeist/browser.rb | 12 ++++++++++-- lib/capybara/poltergeist/client/agent.coffee | 7 +++++-- lib/capybara/poltergeist/client/browser.coffee | 10 ++++++++-- lib/capybara/poltergeist/client/compiled/agent.js | 8 ++++++-- .../poltergeist/client/compiled/browser.js | 12 ++++++++++-- lib/capybara/poltergeist/client/compiled/node.js | 2 +- .../poltergeist/client/compiled/web_page.js | 4 ++++ lib/capybara/poltergeist/client/node.coffee | 2 +- lib/capybara/poltergeist/client/web_page.coffee | 3 +++ lib/capybara/poltergeist/driver.rb | 4 ++++ lib/capybara/poltergeist/node.rb | 15 ++++++++++++--- 11 files changed, 64 insertions(+), 15 deletions(-) diff --git a/lib/capybara/poltergeist/browser.rb b/lib/capybara/poltergeist/browser.rb index ec76591..adef859 100644 --- a/lib/capybara/poltergeist/browser.rb +++ b/lib/capybara/poltergeist/browser.rb @@ -44,6 +44,10 @@ module Capybara::Poltergeist command 'source' end + def title + command 'title' + end + def find(method, selector) result = command('find', method, selector) result['ids'].map { |id| [result['page_id'], id] } @@ -53,8 +57,12 @@ module Capybara::Poltergeist command 'find_within', page_id, id, method, selector end - def text(page_id, id) - command 'text', page_id, id + def all_text(page_id, id) + command 'all_text', page_id, id + end + + def visible_text(page_id, id) + command 'visible_text', page_id, id end def attribute(page_id, id, name) diff --git a/lib/capybara/poltergeist/client/agent.coffee b/lib/capybara/poltergeist/client/agent.coffee index b512057..ce7cd33 100644 --- a/lib/capybara/poltergeist/client/agent.coffee +++ b/lib/capybara/poltergeist/client/agent.coffee @@ -122,8 +122,11 @@ class PoltergeistAgent.Node @element == document.body || document.evaluate('ancestor::body', @element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue - text: -> - if @element.tagName == 'TEXTAREA' + allText: -> + @element.textContent + + visibleText: -> + if @element.nodeName == "TEXTAREA" @element.textContent else @element.innerText diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index 75cda21..e49d3e6 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -98,14 +98,20 @@ class Poltergeist.Browser source: -> this.sendResponse @page.source() + title: -> + this.sendResponse @page.title() + find: (method, selector) -> this.sendResponse(page_id: @page_id, ids: @page.find(method, 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() + all_text: (page_id, id) -> + this.sendResponse this.node(page_id, id).allText() + + visible_text: (page_id, id) -> + this.sendResponse this.node(page_id, id).visibleText() attribute: (page_id, id, name) -> this.sendResponse this.node(page_id, id).getAttribute(name) diff --git a/lib/capybara/poltergeist/client/compiled/agent.js b/lib/capybara/poltergeist/client/compiled/agent.js index 0308e65..3f26aea 100644 --- a/lib/capybara/poltergeist/client/compiled/agent.js +++ b/lib/capybara/poltergeist/client/compiled/agent.js @@ -200,8 +200,12 @@ PoltergeistAgent.Node = (function() { return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue; }; - Node.prototype.text = function() { - if (this.element.tagName === 'TEXTAREA') { + Node.prototype.allText = function() { + return this.element.textContent; + }; + + Node.prototype.visibleText = function() { + if (this.element.nodeName === "TEXTAREA") { return this.element.textContent; } else { return this.element.innerText; diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index 8a24a08..c78ea11 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -124,6 +124,10 @@ Poltergeist.Browser = (function() { return this.sendResponse(this.page.source()); }; + Browser.prototype.title = function() { + return this.sendResponse(this.page.title()); + }; + Browser.prototype.find = function(method, selector) { return this.sendResponse({ page_id: this.page_id, @@ -135,8 +139,12 @@ Poltergeist.Browser = (function() { return this.sendResponse(this.node(page_id, id).find(method, selector)); }; - Browser.prototype.text = function(page_id, id) { - return this.sendResponse(this.node(page_id, id).text()); + Browser.prototype.all_text = function(page_id, id) { + return this.sendResponse(this.node(page_id, id).allText()); + }; + + Browser.prototype.visible_text = function(page_id, id) { + return this.sendResponse(this.node(page_id, id).visibleText()); }; Browser.prototype.attribute = function(page_id, id, name) { diff --git a/lib/capybara/poltergeist/client/compiled/node.js b/lib/capybara/poltergeist/client/compiled/node.js index 4e5f0e5..48cda76 100644 --- a/lib/capybara/poltergeist/client/compiled/node.js +++ b/lib/capybara/poltergeist/client/compiled/node.js @@ -4,7 +4,7 @@ Poltergeist.Node = (function() { var name, _fn, _i, _len, _ref, _this = this; - Node.DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'isVisible', 'position', 'trigger', 'parentId', 'clickTest', 'scrollIntoView', 'isDOMEqual']; + Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'isVisible', 'position', 'trigger', 'parentId', 'clickTest', 'scrollIntoView', 'isDOMEqual']; function Node(page, id) { this.page = page; diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index 4d59141..3074303 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -156,6 +156,10 @@ Poltergeist.WebPage = (function() { return this._source; }; + WebPage.prototype.title = function() { + return this["native"].frameTitle; + }; + WebPage.prototype.errors = function() { return this._errors; }; diff --git a/lib/capybara/poltergeist/client/node.coffee b/lib/capybara/poltergeist/client/node.coffee index af2efc0..853083d 100644 --- a/lib/capybara/poltergeist/client/node.coffee +++ b/lib/capybara/poltergeist/client/node.coffee @@ -1,7 +1,7 @@ # Proxy object for forwarding method calls to the node object inside the page. class Poltergeist.Node - @DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', + @DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'isVisible', 'position', 'trigger', 'parentId', 'clickTest', 'scrollIntoView', 'isDOMEqual'] diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index 416b92d..3957c79 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -101,6 +101,9 @@ class Poltergeist.WebPage source: -> @_source + title: -> + @native.frameTitle + errors: -> @_errors diff --git a/lib/capybara/poltergeist/driver.rb b/lib/capybara/poltergeist/driver.rb index 88b9628..10e3073 100644 --- a/lib/capybara/poltergeist/driver.rb +++ b/lib/capybara/poltergeist/driver.rb @@ -106,6 +106,10 @@ module Capybara::Poltergeist browser.source.to_s end + def title + browser.title + end + def find(method, selector) browser.find(method, selector).map { |page_id, id| Capybara::Poltergeist::Node.new(self, page_id, id) } end diff --git a/lib/capybara/poltergeist/node.rb b/lib/capybara/poltergeist/node.rb index b303c7f..e22442d 100644 --- a/lib/capybara/poltergeist/node.rb +++ b/lib/capybara/poltergeist/node.rb @@ -41,10 +41,13 @@ module Capybara::Poltergeist find :css, selector end - def text - command(:text).gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip + def all_text + filter_text command(:all_text) + end + + def visible_text + filter_text command(:visible_text) end - alias visible_text text def [](name) command :attribute, name @@ -116,5 +119,11 @@ module Capybara::Poltergeist def ==(other) command :equals, other.id end + + private + + def filter_text(text) + text.gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip + end end end