Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Make Node#[] behave more like capybara with selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Nov 8, 2015
1 parent 383d61d commit 33df664
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@
#### Features ####
* Implement support for Capybara Window#size and Window#resize_to (Thomas Walpole)
* Add access to properties of node's native element (Mike Souza)
* Node#[] now prefers element properties over attributes when the property exists and is
not an object. This is similar to the selenium driver behavior. (Thomas Walpole)

#### Bug fixes ####

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/poltergeist/client/browser.coffee
Expand Up @@ -138,7 +138,7 @@ class Poltergeist.Browser
@current_command.sendResponse this.node(page_id, id).deleteText()

property: (page_id, id, name) ->
this.sendResponse this.node(page_id, id).getProperty(name)
@current_command.sendResponse this.node(page_id, id).getProperty(name)

attribute: (page_id, id, name) ->
@current_command.sendResponse this.node(page_id, id).getAttribute(name)
Expand Down
8 changes: 4 additions & 4 deletions lib/capybara/poltergeist/client/compiled/browser.js
Expand Up @@ -180,6 +180,10 @@ Poltergeist.Browser = (function() {
return this.current_command.sendResponse(this.node(page_id, id).deleteText());
};

Browser.prototype.property = function(page_id, id, name) {
return this.current_command.sendResponse(this.node(page_id, id).getProperty(name));
};

Browser.prototype.attribute = function(page_id, id, name) {
return this.current_command.sendResponse(this.node(page_id, id).getAttribute(name));
};
Expand All @@ -188,10 +192,6 @@ Poltergeist.Browser = (function() {
return this.current_command.sendResponse(this.node(page_id, id).getAttributes());
};

Browser.prototype.property = function(page_id, id, name) {
return this.sendResponse(this.node(page_id, id).getProperty(name));
};

Browser.prototype.parents = function(page_id, id) {
return this.current_command.sendResponse(this.node(page_id, id).parentIds());
};
Expand Down
13 changes: 12 additions & 1 deletion lib/capybara/poltergeist/node.rb
Expand Up @@ -55,7 +55,18 @@ def property(name)
end

def [](name)
command :attribute, name
# Although the attribute matters, the property is consistent. Return that in
# preference to the attribute for links and images.
if (tag_name == 'img' and name == 'src') or (tag_name == 'a' and name == 'href' )
#if attribute exists get the property
value = command(:attribute, name) && command(:property, name)
return value
end

value = property(name)
value = command(:attribute, name) if value.nil? || value.is_a?(Hash)

value
end

def attributes
Expand Down
44 changes: 40 additions & 4 deletions spec/integration/session_spec.rb
Expand Up @@ -257,6 +257,42 @@
end
end

describe 'Node#checked?' do
before do
@session.visit '/poltergeist/attributes_properties'
end

it 'is a boolean' do
expect(@session.find_field('checked').checked?).to be true
expect(@session.find_field('unchecked').checked?).to be false
end
end

describe 'Node#[]' do
before do
@session.visit '/poltergeist/attributes_properties'
end

it 'gets normalized href' do
expect(@session.find(:link, 'Loop')['href']).to eq("http://#{@session.server.host}:#{@session.server.port}/poltergeist/attributes_properties")
end

it 'gets innerHTML' do
expect(@session.find(:css,'.some_other_class')['innerHTML']).to eq '<p>foobar</p>'
end

it 'gets attribute' do
link = @session.find(:link, 'Loop')
expect(link['data-random']).to eq '42'
expect(link['onclick']).to eq "return false;"
end

it 'gets boolean attributes as booleans' do
expect(@session.find_field('checked')['checked']).to be true
expect(@session.find_field('unchecked')['checked']).to be false
end
end

it 'has no trouble clicking elements when the size of a document changes' do
@session.visit('/poltergeist/long_page')
@session.find(:css, '#penultimate').click
Expand Down Expand Up @@ -737,20 +773,20 @@

context 'supports accessing element properties' do
before do
@session.visit '/poltergeist/property'
@session.visit '/poltergeist/attributes_properties'
end

it 'gets property innerHTML' do
expect(@session.find(:css,'.some_class').native.property('innerHTML')).to eq '<p>foobar</p>'
expect(@session.find(:css,'.some_other_class').native.property('innerHTML')).to eq '<p>foobar</p>'
end

it 'gets property outerHTML' do
expect(@session.find(:css,'.some_class').native.property('outerHTML')).to eq '<div class="some_class"><p>foobar</p></div>'
expect(@session.find(:css,'.some_other_class').native.property('outerHTML')).to eq '<div class="some_other_class"><p>foobar</p></div>'
end
end

it 'allows access to element attributes' do
@session.visit '/poltergeist/attributes'
@session.visit '/poltergeist/attributes_properties'
expect(@session.find(:css,'#my_link').native.attributes).to eq(
'href' => '#', 'id' => 'my_link', 'class' => 'some_class', 'data' => 'rah!'
)
Expand Down
6 changes: 0 additions & 6 deletions spec/support/views/attributes.erb

This file was deleted.

10 changes: 10 additions & 0 deletions spec/support/views/attributes_properties.erb
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>
<a href="#" id="my_link" class="some_class" data="rah!">Link me</a>
<div class="some_other_class"><p>foobar</p></div>
<a href="/poltergeist/attributes_properties" onclick="return false;" data-random="42">Loop</a>
<input type="checkbox" id="checked" checked="checked"/>
<input type="checkbox" id="unchecked"/>
</body>
</html>
6 changes: 0 additions & 6 deletions spec/support/views/property.erb

This file was deleted.

0 comments on commit 33df664

Please sign in to comment.