1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Implement all_text and visible_text

This commit is contained in:
Jon Leighton 2013-03-03 14:21:12 +00:00
parent 7e216ee0f5
commit 9add4cc83d
11 changed files with 64 additions and 15 deletions

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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;

View file

@ -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) {

View file

@ -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;

View file

@ -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;
};

View file

@ -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']

View file

@ -101,6 +101,9 @@ class Poltergeist.WebPage
source: ->
@_source
title: ->
@native.frameTitle
errors: ->
@_errors

View file

@ -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

View file

@ -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