How to handle tinyMCE when automating with watir-webdriver?
Asked Answered
S

3

7

I'm evaluating Watir-webdriver, to decide if i can switch to using it for my browser tests (from Watir mostly) and one of the key things would be the ability to interact with TinyMCE WYSIWYG editors, as a number of the applications I work with use TinyMCE. I've managed to get the following solution working -

@browser = Watir::Browser.new(:firefox)
@browser.goto("http://tinymce.moxiecode.com/tryit/full.php")
autoit = WIN32OLE.new('AutoITX3.Control')
autoit.WinActivate('TinyMCE - TinyMCE - Full featured example')
@browser.frame(:index, 0).body.click
autoit.Send("^a") # CTRL + a to select all
autoit.Send("{DEL}")
autoit.Send("Some new text")

The drawback of this approach, is that by using autoit, I remain dependent on Windows and the ability to run tests cross-platform is one of the attractions of webdriver.

I noticed some webdriver specific solutions such as the following from this thread:

String tinyMCEFrame = "TextEntryFrameName" // Replace as necessary
this.getDriver().switchTo().frame(tinyMCEFrame);
String entryText = "Testing entry\r\n";
this.getDriver().findElement(By.id("tinymce")).sendKeys(entryText);
//Replace ID as necessary
this.getDriver().switchTo().window(this.getDriver().getWindowHandle());
try {
  Thread.sleep(3000);
} catch (InterruptedException e) {

  e.printStackTrace();
}

this.getDriver().findElement(By.partialLinkText("Done")).click(); 

Which looks like it might work cross-platform but I don't know if the same functionality can be accessed from within Watir-webdriver. My question is, is there a way to write, delete and submit into TinyMCE using watir-webdriver, which will not enforce a dependency on a specific supported browser or operating system?

Seasonseasonable answered 12/1, 2011 at 0:16 Comment(1)
consider posting this question in the tinymce forum of moxiecode too (this is very specific)Estrus
S
5

At the moment, you'll need to reach in and get the underlying driver instance. This works for me on the TinyMCE example page

b = Watir::Browser.new
b.goto "http://tinymce.moxiecode.com/tryit/full.php"

d = b.driver
d.switch_to.frame "content_ifr"
d.switch_to.active_element.send_keys "hello world"

This is actually not well exposed in watir-webdriver, but I'll fix that. After the next release (0.1.9) you should be able to simply do:

b.frame(:id => "content_ifr").send_keys "hello world"
Sapheaded answered 13/1, 2011 at 10:58 Comment(4)
Perfect. This is exactly the answer that I thought may have been possible but lacked/ couldn't find the knowledge to apply. I look forward to the next release.Seasonseasonable
Almost perfect. How to clear previous content? MCE is already populated. Focus is in the beginning, so sending "\b" does nothing. I failed to send other control characters/sequences. For now I have to settle with prepending new text...Leopoldine
Wojciech: This works on my Mac - .send_keys [:command, 'a'], :backspace, "hello world" - on other platforms, you probably want :control instead of :command.Sapheaded
Wojciech: On Windows I'm using d.switch_to.active_element.send_keys("\ca") which select all the text, then when I'm sending my new content its over-writing what was there before. Although this exactly the same as Jarib's answer above.Seasonseasonable
Y
2

I find a better way of automating TinyMCE editors is to call the JavaScript API directly, that way you avoid having the use the iFrames which I find troublesome.

For example:

require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'http://tinymce.moxiecode.com/tryit/full.php'
b.execute_script("tinyMCE.get('content').execCommand('mceSetContent',false, 'hello world' );")

See: http://watirwebdriver.com/wysiwyg-editors/

Yeargain answered 18/9, 2011 at 12:8 Comment(0)
S
0

On more recent versions of TinyMCE (notably the one currently on the Moxiecode Full Featured example used in the example above) it seems you need to add a .click into the script to select the text area after the backspace, so you might need to use something like:

browser.frame(:id, "content_ifr").send_keys [:control, "a"], :backspace
browser.frame(:id, "content_ifr").click
browser.frame(:id, "content_ifr").send_keys("Hello World")
Seasonseasonable answered 14/6, 2011 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.