Cucumber: Wait until CKEditor is loaded

I had to deal with JavaScript Undefined Error while accessing a specific CKEditor instance to fill in text.

Ensure everything is loaded with

patiently do
  page.execute_script("return isCkeditorLoaded('#{selector}');").should be_true
end

Example

The fill in text snippet for Cucumber:

When /^I fill in the "([^\"]+)" WYSIWYG editor with:$/ do |selector, html|
  patiently do
    page.execute_script("return isCkeditorLoaded('#{selector}');").should be_true
  end
  html.gsub!(/\n+/, "") # otherwise: unterminated string literal (Selenium::WebDriver::Error::JavascriptError)
  page.execute_script("CKEDITOR.instances['#{selector}'].setData('#{html}');")
end

The snippet for JavaScript:

function isCkeditorLoaded(instance_selector) {
  // instance_selector is e.g. 'template_html'
  var status;
  if (window.CKEDITOR && CKEDITOR.instances && CKEDITOR.instances[instance_selector]) {
    status = CKEDITOR.instances[instance_selector].status;
  }
  return status === 'ready';
};
Martin Straub