Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Created September 9, 2014 23:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@avdeev
Copy link

avdeev commented Oct 27, 2014

Failure/Error: tinymce_fill_in "#{form_scope}_content", new_content
Capybara::Webkit::InvalidResponseError:
Javascript failed to execute

@obromios
Copy link

This worked for me, thank you.

However,t if you are improving it, you might like to consider testing if id exists. Currently, if the id does not exist, then the test just hangs.

Copy link

ghost commented May 24, 2016

thanks! worked for me. I spent a whole day trying to figure this out and then I found your helper. and now I feel stupid for waiting a whole day to look on stackoverflow

@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