Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Created September 9, 2014 23:53
Show Gist options
  • Save eoinkelly/69be6c27beb0106aa555 to your computer and use it in GitHub Desktop.
Save eoinkelly/69be6c27beb0106aa555 to your computer and use it in GitHub Desktop.
A capybara helper to fill in text into a TinyMCE editor instance
##
# id must be the id attribute of the editor instance (without the #) e.g.
# <textarea id="foo" ...></textarea>
# would be filled in by calling
# tinymce_fill_in 'foo', 'some stuff'
#
def tinymce_fill_in(id, val)
# wait until the TinyMCE editor instance is ready. This is required for cases
# where the editor is loaded via XHR.
sleep 0.5 until page.evaluate_script("tinyMCE.get('#{id}') !== null")
js = "tinyMCE.get('#{id}').setContent('#{val}')"
page.execute_script(js)
end
@EverybodyKurts
Copy link

If you have any sort of code snippet or latex, this (very helpful) helper method might not work for you. If I come across a / up with a solution, I'll post it here.

@kjoscha
Copy link

kjoscha commented Apr 16, 2019

I recommend to use expect(page).to have_css('#tinymce-editor_ifr') (this waits for an element created by tinymce isntance) instead of sleep 0.5 to make waiting für tinymce more solid and fast

@lbraun
Copy link

lbraun commented Sep 4, 2019

Thanks @eoinkelly and @kjoscha! I adapted it slightly to work with minitest and mimic the syntax of the regular fill_in method:

  # `id` must be the id attribute of the editor instance e.g.
  # <textarea id="foo" ...></textarea>
  def tinymce_fill_in(id, with:)
    assert_css("##{id}_ifr")
    js = "tinyMCE.get('#{id}').setContent('#{with}')"
    page.execute_script(js)
  end

@elissonmichael
Copy link

elissonmichael commented Oct 21, 2020

Thanks for sharing @lbraun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment