Skip to content

Commit

Permalink
Merge pull request teampoltergeist#448 from JonRowe/access_node_parents
Browse files Browse the repository at this point in the history
  • Loading branch information
thedelchop committed Apr 14, 2014
2 parents b27e744 + 0054d0b commit 1f0c785
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def title
command 'title'
end

def parents(page_id, id)
command 'parents', page_id, id
end

def find(method, selector)
result = command('find', method, selector)
result['ids'].map { |id| [result['page_id'], id] }
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/poltergeist/client/agent.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class PoltergeistAgent.Node
parentId: ->
@agent.register(@element.parentNode)

parentIds: ->
ids = []
parent = @element.parentNode
while parent != document
ids.push @agent.register(parent)
parent = parent.parentNode
ids

find: (method, selector) ->
@agent.find(method, selector, @element)

Expand Down
3 changes: 3 additions & 0 deletions lib/capybara/poltergeist/client/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class Poltergeist.Browser
attributes: (page_id, id, name) ->
this.sendResponse this.node(page_id, id).getAttributes()

parents: (page_id, id) ->
this.sendResponse this.node(page_id, id).parentIds()

value: (page_id, id) ->
this.sendResponse this.node(page_id, id).value()

Expand Down
11 changes: 11 additions & 0 deletions lib/capybara/poltergeist/client/compiled/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ PoltergeistAgent.Node = (function() {
return this.agent.register(this.element.parentNode);
};

Node.prototype.parentIds = function() {
var ids, parent;
ids = [];
parent = this.element.parentNode;
while (parent !== document) {
ids.push(this.agent.register(parent));
parent = parent.parentNode;
}
return ids;
};

Node.prototype.find = function(method, selector) {
return this.agent.find(method, selector, this.element);
};
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/client/compiled/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ Poltergeist.Browser = (function() {
return this.sendResponse(this.node(page_id, id).getAttributes());
};

Browser.prototype.parents = function(page_id, id) {
return this.sendResponse(this.node(page_id, id).parentIds());
};

Browser.prototype.value = function(page_id, id) {
return this.sendResponse(this.node(page_id, id).value());
};
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/poltergeist/client/compiled/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var __slice = [].slice;
Poltergeist.Node = (function() {
var name, _fn, _i, _len, _ref;

Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'getAttributes', 'isVisible', 'position', 'trigger', 'parentId', 'mouseEventTest', 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText'];
Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'getAttributes', 'isVisible', 'position', 'trigger', 'parentId', 'parentIds', 'mouseEventTest', 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText'];

function Node(page, id) {
this.page = page;
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/poltergeist/client/node.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Poltergeist.Node
@DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete',
'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'getAttributes',
'isVisible', 'position', 'trigger', 'parentId', 'mouseEventTest',
'isVisible', 'position', 'trigger', 'parentId', 'parentIds', 'mouseEventTest',
'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText']

constructor: (@page, @id) ->
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def command(name, *args)
end
end

def parents
command(:parents).map { |parent_id| self.class.new(driver, page_id, parent_id) }
end

def find(method, selector)
command(:find_within, method, selector).map { |id| self.class.new(driver, page_id, id) }
end
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,5 +583,11 @@
'href' => '#', 'id' => 'my_link', 'class' => 'some_class', 'data' => 'rah!'
)
end

it "knows about its parents" do
@session.visit '/poltergeist/simple'
parents = @session.find(:css,'#nav').native.parents
expect(parents.map(&:tag_name)).to eq ['li','ul','body','html']
end
end
end
3 changes: 3 additions & 0 deletions spec/support/views/simple.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
</head>

<body>
<ul>
<li><a id="nav" href="/">Home</a></li>
</ul>
<a href="/">Link</a>

<p id="break">Foo<br>Bar</p>
Expand Down

0 comments on commit 1f0c785

Please sign in to comment.