使用watir-webdriver自动化时如何处理tinyMCE?

时间:2011-01-12 00:16:02

标签: tinymce watir webdriver watir-webdriver

我正在评估Watir-webdriver,以决定我是否可以切换到使用它进行我的浏览器测试(主要来自Watir),关键之一就是能够与TinyMCE WYSIWYG编辑器进行交互,作为一些我使用的应用程序使用TinyMCE。  我设法让以下解决方案正常工作 -

@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")

这种方法的缺点是,通过使用autoit,我仍然依赖于Windows,跨平台运行测试的能力是webdriver的吸引力之一。

我注意到了一些特定于webdriver的解决方案,例如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(); 

看起来它可能跨平台工作,但我不知道是否可以从Watir-webdriver中访问相同的功能。我的问题是,有没有办法使用watir-webdriver编写,删除和提交到TinyMCE,这不会强制依赖特定支持的浏览器或操作系统?

3 个答案:

答案 0 :(得分:5)

目前,您需要进入并获取底层驱动程序实例。这在TinyMCE示例页面上适用于我

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"

这实际上并没有在watir-webdriver中公开,但我会解决这个问题。在下一个版本(0.1.9)之后,你应该能够做到:

b.frame(:id => "content_ifr").send_keys "hello world"

答案 1 :(得分:2)

我发现更好的自动化TinyMCE编辑器的方法是直接调用JavaScript API,这样就可以避免使用我觉得麻烦的iFrame。

例如:

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' );")

请参阅:http://watirwebdriver.com/wysiwyg-editors/

答案 2 :(得分:0)

在更新版本的TinyMCE(特别是上面示例中使用的Moxiecode全功能示例中)上,您似乎需要在脚本中添加.click以选择退格后的文本区域,这样您就可以了需要使用类似的东西:

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")