Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle tinyMCE when automating with watir-webdriver?

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?

like image 840
sean_robbins Avatar asked Jan 12 '11 00:01

sean_robbins


2 Answers

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"
like image 193
jarib Avatar answered Oct 27 '22 23:10

jarib


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/

like image 2
Alister Scott Avatar answered Oct 28 '22 00:10

Alister Scott