Chrome changed the JS behavior of Selection#addRange

This commit is contained in:
Thomas Walpole 2017-04-23 13:51:25 -07:00
parent 084cf8eded
commit 07f14cf707
2 changed files with 23 additions and 17 deletions

View File

@ -77,16 +77,20 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
elsif native.attribute('isContentEditable')
#ensure we are focused on the element
native.click
script = <<-JS
var range = document.createRange();
var sel = window.getSelection();
arguments[0].focus();
range.selectNodeContents(arguments[0]);
window.getSelection().addRange(range);
sel.removeAllRanges();
sel.addRange(range);
JS
driver.execute_script script, self
if (driver.options[:browser].to_s == "chrome") ||
(driver.options[:browser].to_s == "firefox" && !driver.marionette?)
# chromedriver raises a can't focus element if we use native.send_keys
# chromedriver raises a can't focus element for child elements if we use native.send_keys
# we've already focused it so just use action api
driver.browser.action.send_keys(value.to_s).perform
else

View File

@ -122,23 +122,25 @@ Capybara::SpecHelper.spec "node" 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')
expect(@session.find(:css,'#existing_content_editable').text).to eq('WYSIWYG')
end
context "with a contenteditable element", requires: [:js] do
it 'should allow me to change the contents' do
@session.visit('/with_js')
@session.find(:css,'#existing_content_editable').set('WYSIWYG')
expect(@session.find(:css,'#existing_content_editable').text).to eq('WYSIWYG')
end
it 'should allow me to set the contents of a contenteditable element', requires: [:js] do
@session.visit('/with_js')
@session.find(:css,'#blank_content_editable').set('WYSIWYG')
expect(@session.find(:css,'#blank_content_editable').text).to eq('WYSIWYG')
end
it 'should allow me to set the contents' do
@session.visit('/with_js')
@session.find(:css,'#blank_content_editable').set('WYSIWYG')
expect(@session.find(:css,'#blank_content_editable').text).to eq('WYSIWYG')
end
it 'should allow me to change the contents of a contenteditable elements child', requires: [:js] do
@session.visit('/with_js')
@session.find(:css,'#existing_content_editable_child').set('WYSIWYG')
expect(@session.find(:css,'#existing_content_editable_child').text).to eq('WYSIWYG')
expect(@session.find(:css,'#existing_content_editable_child_parent').text).to eq('Some content WYSIWYG')
it 'should allow me to change the contents of a child element' do
@session.visit('/with_js')
@session.find(:css,'#existing_content_editable_child').set('WYSIWYG')
expect(@session.find(:css,'#existing_content_editable_child').text).to eq('WYSIWYG')
expect(@session.find(:css,'#existing_content_editable_child_parent').text).to eq('Some content WYSIWYG')
end
end
end