Permalink
Browse files

Support filling in `contenteditable` elements, closes #911

  • Loading branch information...
1 parent 6b1e42d commit 3f5180ffcdd954524fb33b8cb9fab2be77e060dd @JonRowe JonRowe committed with jnicklas Dec 15, 2012
@@ -40,6 +40,15 @@ def set(value)
#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]
native.send_keys(value.to_s)
+ elsif native.attribute('isContentEditable')
+ #ensure we are focused on the element
+ script = <<-JS
+ var range = document.createRange();
+ range.selectNodeContents(arguments[0]);
+ window.getSelection().addRange(range);
+ JS
+ driver.browser.execute_script script, native
+ native.send_keys(value.to_s)
end
end
@@ -61,7 +70,7 @@ def click
def hover
driver.browser.action.move_to(native).perform
end
-
+
def drag_to(element)
driver.browser.action.drag_and_drop(native, element.native).perform
end
@@ -89,11 +98,11 @@ def disabled?
def find_xpath(locator)
native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
end
-
+
def find_css(locator)
native.find_elements(:css, locator).map { |n| self.class.new(driver, n) }
end
-
+
def ==(other)
native == other.native
end
@@ -44,7 +44,7 @@
@session.click_button('awesome')
extract_results(@session)['description'].should == 'Texty text'
end
-
+
it "should fill in a password field by id" do
@session.fill_in('form_password', :with => 'supasikrit')
@session.click_button('awesome')
@@ -56,7 +56,7 @@
@session.click_button('awesome')
extract_results(@session)['description'].should == 'is <strong>very</strong> secret!'
end
-
+
it "should handle newlines in a textarea", tw: true do
@session.fill_in('form_description', :with => "\nSome text\n")
@session.click_button('awesome')
@@ -62,7 +62,7 @@
@session.find('//textarea[@id="normal"]').set("\nbanana")
@session.find('//textarea[@id="normal"]').value.should == "\nbanana"
end
-
+
it "return any HTML content in textarea" do
@session.find('//textarea[1]').set("some <em>html</em> here")
@session.find('//textarea[1]').value.should == "some <em>html</em> here"
@@ -93,6 +93,25 @@
@session.first('//textarea[@readonly]').set('changed')
@session.first('//textarea[@readonly]').value.should == 'textarea should not change'
end
+
+ 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')
+ @session.find(:css,'#existing_content_editable').text.should == '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')
+ @session.find(:css,'#blank_content_editable').text.should == 'WYSIWYG'
+ end
+
+ it 'should allow me to change the contents of a contenteditable elements child', :requires => [:js] do
+ pending "Selenium doesn't like editing nested contents"
+ @session.visit('/with_js')
+ @session.find(:css,'#existing_content_editable_child').set('WYSIWYG')
+ @session.find(:css,'#existing_content_editable_child').text.should == 'WYSIWYG'
+ end
end
describe "#tag_name" do
@@ -37,6 +37,14 @@
</p>
<p>
+ <div contenteditable='true' id='existing_content_editable'>Editable content</div>
+ <div contenteditable='true' id='blank_content_editable' style='height: 1em;'></div>
+ <div contenteditable='true' style='height: 1em;'>
+ <div id='existing_content_editable_child' style='height: 1em;'>Content</div>
+ </div>
+ </p>
+
+ <p>
<input type="checkbox" id="checkbox_with_event"/>
</p>

0 comments on commit 3f5180f

Please sign in to comment.