Setting values of CKEditor textboxes via Selenium

I needed to test a site’s commenting functionality.
The way that this site would do this is using a lot of javascript while using CKEditor, which does its own javascript stuff in the background.

Because CKEditor does stuff to the textbox html you attach it to, just hooking onto that textbox and setting the value is not going to work.
I found a CKEditor API call that will let you set the value as long as you know the id of the div that CKEditor wraps the generated textbox with. In my case, the name of the div is called “remedy609”. Yours will likely be different.

Selenium allows you to execute your own javascript as if you were doing it on a browser console. So putting these two things together allows you to set a CKEditor text box like so:

val driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24)
driver.setJavascriptEnabled(true)

val commentText = s”This is an issue generated by a selenium test ${UUID.randomUUID().toString}”

val javascriptExecutor = driver.asInstanceOf[JavascriptExecutor]
javascriptExecutor.executeScript(s”CKEDITOR.instances.remedy609.setData(‘<p>$commentText</p>’)”)

Leave a comment