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:
parent
7e216ee0f5
commit
9add4cc83d
11 changed files with 64 additions and 15 deletions
|
@ -44,6 +44,10 @@ module Capybara::Poltergeist
|
||||||
command 'source'
|
command 'source'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def title
|
||||||
|
command 'title'
|
||||||
|
end
|
||||||
|
|
||||||
def find(method, selector)
|
def find(method, selector)
|
||||||
result = command('find', method, selector)
|
result = command('find', method, selector)
|
||||||
result['ids'].map { |id| [result['page_id'], id] }
|
result['ids'].map { |id| [result['page_id'], id] }
|
||||||
|
@ -53,8 +57,12 @@ module Capybara::Poltergeist
|
||||||
command 'find_within', page_id, id, method, selector
|
command 'find_within', page_id, id, method, selector
|
||||||
end
|
end
|
||||||
|
|
||||||
def text(page_id, id)
|
def all_text(page_id, id)
|
||||||
command 'text', page_id, id
|
command 'all_text', page_id, id
|
||||||
|
end
|
||||||
|
|
||||||
|
def visible_text(page_id, id)
|
||||||
|
command 'visible_text', page_id, id
|
||||||
end
|
end
|
||||||
|
|
||||||
def attribute(page_id, id, name)
|
def attribute(page_id, id, name)
|
||||||
|
|
|
@ -122,8 +122,11 @@ class PoltergeistAgent.Node
|
||||||
@element == document.body ||
|
@element == document.body ||
|
||||||
document.evaluate('ancestor::body', @element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue
|
document.evaluate('ancestor::body', @element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue
|
||||||
|
|
||||||
text: ->
|
allText: ->
|
||||||
if @element.tagName == 'TEXTAREA'
|
@element.textContent
|
||||||
|
|
||||||
|
visibleText: ->
|
||||||
|
if @element.nodeName == "TEXTAREA"
|
||||||
@element.textContent
|
@element.textContent
|
||||||
else
|
else
|
||||||
@element.innerText
|
@element.innerText
|
||||||
|
|
|
@ -98,14 +98,20 @@ class Poltergeist.Browser
|
||||||
source: ->
|
source: ->
|
||||||
this.sendResponse @page.source()
|
this.sendResponse @page.source()
|
||||||
|
|
||||||
|
title: ->
|
||||||
|
this.sendResponse @page.title()
|
||||||
|
|
||||||
find: (method, selector) ->
|
find: (method, selector) ->
|
||||||
this.sendResponse(page_id: @page_id, ids: @page.find(method, selector))
|
this.sendResponse(page_id: @page_id, ids: @page.find(method, selector))
|
||||||
|
|
||||||
find_within: (page_id, id, method, selector) ->
|
find_within: (page_id, id, method, selector) ->
|
||||||
this.sendResponse this.node(page_id, id).find(method, selector)
|
this.sendResponse this.node(page_id, id).find(method, selector)
|
||||||
|
|
||||||
text: (page_id, id) ->
|
all_text: (page_id, id) ->
|
||||||
this.sendResponse this.node(page_id, id).text()
|
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) ->
|
attribute: (page_id, id, name) ->
|
||||||
this.sendResponse this.node(page_id, id).getAttribute(name)
|
this.sendResponse this.node(page_id, id).getAttribute(name)
|
||||||
|
|
|
@ -200,8 +200,12 @@ PoltergeistAgent.Node = (function() {
|
||||||
return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
|
return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
Node.prototype.text = function() {
|
Node.prototype.allText = function() {
|
||||||
if (this.element.tagName === 'TEXTAREA') {
|
return this.element.textContent;
|
||||||
|
};
|
||||||
|
|
||||||
|
Node.prototype.visibleText = function() {
|
||||||
|
if (this.element.nodeName === "TEXTAREA") {
|
||||||
return this.element.textContent;
|
return this.element.textContent;
|
||||||
} else {
|
} else {
|
||||||
return this.element.innerText;
|
return this.element.innerText;
|
||||||
|
|
|
@ -124,6 +124,10 @@ Poltergeist.Browser = (function() {
|
||||||
return this.sendResponse(this.page.source());
|
return this.sendResponse(this.page.source());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Browser.prototype.title = function() {
|
||||||
|
return this.sendResponse(this.page.title());
|
||||||
|
};
|
||||||
|
|
||||||
Browser.prototype.find = function(method, selector) {
|
Browser.prototype.find = function(method, selector) {
|
||||||
return this.sendResponse({
|
return this.sendResponse({
|
||||||
page_id: this.page_id,
|
page_id: this.page_id,
|
||||||
|
@ -135,8 +139,12 @@ Poltergeist.Browser = (function() {
|
||||||
return this.sendResponse(this.node(page_id, id).find(method, selector));
|
return this.sendResponse(this.node(page_id, id).find(method, selector));
|
||||||
};
|
};
|
||||||
|
|
||||||
Browser.prototype.text = function(page_id, id) {
|
Browser.prototype.all_text = function(page_id, id) {
|
||||||
return this.sendResponse(this.node(page_id, id).text());
|
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) {
|
Browser.prototype.attribute = function(page_id, id, name) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ Poltergeist.Node = (function() {
|
||||||
var name, _fn, _i, _len, _ref,
|
var name, _fn, _i, _len, _ref,
|
||||||
_this = this;
|
_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) {
|
function Node(page, id) {
|
||||||
this.page = page;
|
this.page = page;
|
||||||
|
|
|
@ -156,6 +156,10 @@ Poltergeist.WebPage = (function() {
|
||||||
return this._source;
|
return this._source;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WebPage.prototype.title = function() {
|
||||||
|
return this["native"].frameTitle;
|
||||||
|
};
|
||||||
|
|
||||||
WebPage.prototype.errors = function() {
|
WebPage.prototype.errors = function() {
|
||||||
return this._errors;
|
return this._errors;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Proxy object for forwarding method calls to the node object inside the page.
|
# Proxy object for forwarding method calls to the node object inside the page.
|
||||||
|
|
||||||
class Poltergeist.Node
|
class Poltergeist.Node
|
||||||
@DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete',
|
@DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete',
|
||||||
'removeAttribute', 'isMultiple', 'select', 'tagName', 'find',
|
'removeAttribute', 'isMultiple', 'select', 'tagName', 'find',
|
||||||
'isVisible', 'position', 'trigger', 'parentId', 'clickTest',
|
'isVisible', 'position', 'trigger', 'parentId', 'clickTest',
|
||||||
'scrollIntoView', 'isDOMEqual']
|
'scrollIntoView', 'isDOMEqual']
|
||||||
|
|
|
@ -101,6 +101,9 @@ class Poltergeist.WebPage
|
||||||
source: ->
|
source: ->
|
||||||
@_source
|
@_source
|
||||||
|
|
||||||
|
title: ->
|
||||||
|
@native.frameTitle
|
||||||
|
|
||||||
errors: ->
|
errors: ->
|
||||||
@_errors
|
@_errors
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,10 @@ module Capybara::Poltergeist
|
||||||
browser.source.to_s
|
browser.source.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def title
|
||||||
|
browser.title
|
||||||
|
end
|
||||||
|
|
||||||
def find(method, selector)
|
def find(method, selector)
|
||||||
browser.find(method, selector).map { |page_id, id| Capybara::Poltergeist::Node.new(self, page_id, id) }
|
browser.find(method, selector).map { |page_id, id| Capybara::Poltergeist::Node.new(self, page_id, id) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,10 +41,13 @@ module Capybara::Poltergeist
|
||||||
find :css, selector
|
find :css, selector
|
||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def all_text
|
||||||
command(:text).gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip
|
filter_text command(:all_text)
|
||||||
|
end
|
||||||
|
|
||||||
|
def visible_text
|
||||||
|
filter_text command(:visible_text)
|
||||||
end
|
end
|
||||||
alias visible_text text
|
|
||||||
|
|
||||||
def [](name)
|
def [](name)
|
||||||
command :attribute, name
|
command :attribute, name
|
||||||
|
@ -116,5 +119,11 @@ module Capybara::Poltergeist
|
||||||
def ==(other)
|
def ==(other)
|
||||||
command :equals, other.id
|
command :equals, other.id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def filter_text(text)
|
||||||
|
text.gsub(NBSP, ' ').gsub(/\s+/u, ' ').strip
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue