mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00
implement find_css
This commit is contained in:
parent
eedb66d1d0
commit
5ba99efc53
7 changed files with 50 additions and 24 deletions
|
@ -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)
|
||||
|
|
|
@ -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) =>
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue