Skip to content

Instantly share code, notes, and snippets.

@bbugh
Created October 20, 2017 20:34
Show Gist options
  • Save bbugh/4592b91291509ccd3e8684bfc2d7fa7d to your computer and use it in GitHub Desktop.
Save bbugh/4592b91291509ccd3e8684bfc2d7fa7d to your computer and use it in GitHub Desktop.
Improved handling of TinyMCE with error logging and guard checks
module TinyMCESpecHelper
# Fill in a TinyMCE editor with the specified value.
# Pass in the same id you give to TinyMCE, (such as "editorContent")
# *not* TinyMCE's generated one.
#
# NOTE: The first argument is not a css selector, just an element id.
#
# May require selenium chromedriver!
# Adapted from https://gist.github.com/eoinkelly/69be6c27beb0106aa555
#
def fill_in_tinymce(editor_id, value)
expect(page).to have_selector("##{editor_id}", visible: false)
# wait until the TinyMCE editor instance is ready. This is required for cases
# where the editor is loaded via XHR.
count = 0
until page.evaluate_script("tinyMCE.get('#{editor_id}') !== null")
sleep 0.5
if (count += 1) > 10 # rubocop:disable Style/Next
log = page.driver.browser.manage.logs.get('browser')
errors = log.map.with_index { |e, i| "#{i + 1}: #{e}" }.join("\n")
raise "cannot find TinyMCE editor:\n#{errors}"
end
end
js = "tinyMCE.get('#{editor_id}').setContent('#{value}')"
page.execute_script(js)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment