Warn when attempting to set a value to a readonly field

This commit is contained in:
Thomas Walpole 2014-06-11 17:14:31 -07:00
parent 5e12101ad3
commit e9c531fd18
4 changed files with 23 additions and 4 deletions

View File

@ -15,6 +15,7 @@ module Capybara
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

View File

@ -27,7 +27,11 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
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
@ -165,7 +169,11 @@ private
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

View File

@ -37,11 +37,13 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
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')

View File

@ -97,6 +97,10 @@ Capybara::SpecHelper.spec "node" do
@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')
@ -104,6 +108,10 @@ Capybara::SpecHelper.spec "node" do
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')