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>’)”)

Advertisements

A reason why TryUpdateModel or UpdateModel can pass yet not correctly update the model’s properties.

I just spent over an hour wondering why a post controller function’s call to UpdateModel was not updating the view model’s properties with the values in the form collection.

The mysterious thing was that UpdateModel was not throwing an exception which I as the caller would expect if it does not actually update the properties.

It turns out that the view model had been refactored a tiny bit recently and the relevant properties had their setter made private.
Usually when something wants to modify a property with a private set, a compiler error will appear informing you about this. This was not the case here sadly.

So as a reminder to myself,

When UpdateModel or TryUpdateModel does not update correctly, first thing to do is check that the relevant view model’s properties setters are public.