Skip to content

Commit

Permalink
Warn when attempting to set a value to a readonly field
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Jun 12, 2014
1 parent 5e12101 commit e9c531f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NotSupportedByDriverError < CapybaraError; end
class InfiniteRedirectError < CapybaraError; end
class ScopeError < CapybaraError; end
class WindowError < CapybaraError; end
class ReadOnlyElementError < CapybaraError; end

class << self
attr_accessor :asset_host, :app_host, :run_server, :default_host, :always_include_port
Expand Down
12 changes: 10 additions & 2 deletions lib/capybara/rack_test/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def set(value)
elsif input_field?
set_input(value)
elsif textarea?
native.content = value.to_s unless self[:readonly]
if self[:readonly]
warn "Attempt to set readonly element with value: #{value} \n * This will raise an exception in a future version of Capybara"
else
native.content = value.to_s
end
end
end

Expand Down Expand Up @@ -165,7 +169,11 @@ def set_input(value)
end
native.remove
else
native['value'] = value.to_s unless self[:readonly]
if self[:readonly]
warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
else
native['value'] = value.to_s
end
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/capybara/selenium/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ def set(value)
path_names = value.to_s.empty? ? [] : value
native.send_keys(*path_names)
elsif tag_name == 'textarea' or tag_name == 'input'
if value.to_s.empty?
if self[:readonly]
warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
elsif value.to_s.empty?
native.clear
else
#script can change a readonly element which user input cannot, so dont execute if readonly
driver.browser.execute_script "arguments[0].value = ''", native unless self[:readonly]
driver.browser.execute_script "arguments[0].value = ''", native
native.send_keys(value.to_s)
end
elsif native.attribute('isContentEditable')
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/spec/session/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,21 @@
@session.first('//input[@readonly]').set('changed')
expect(@session.first('//input[@readonly]').value).to eq('should not change')
end

it "should raise if the text field is readonly" do
expect(@session.first('//input[@readonly]').set('changed')).to raise_error(Capybara::ReadOnlyElementError)
end if Capybara::VERSION.to_f > 3.0

it "should not set if the textarea is readonly" do
expect(@session.first('//textarea[@readonly]').value).to eq('textarea should not change')
@session.first('//textarea[@readonly]').set('changed')
expect(@session.first('//textarea[@readonly]').value).to eq('textarea should not change')
end

it "should raise if the text field is readonly" do
expect(@session.first('//textarea[@readonly]').set('changed')).to raise_error(Capybara::ReadOnlyElementError)
end if Capybara::VERSION.to_f > 3.0

it 'should allow me to change the contents of a contenteditable element', :requires => [:js] do
@session.visit('/with_js')
@session.find(:css,'#existing_content_editable').set('WYSIWYG')
Expand Down

0 comments on commit e9c531f

Please sign in to comment.