【问题标题】:How to handle tinyMCE when automating with watir-webdriver?使用 watir-webdriver 自动化时如何处理 tinyMCE?
【发布时间】:2011-09-18 20:15:33
【问题描述】:

我正在评估 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,这不会强制依赖特定支持的浏览器或操作系统?

【问题讨论】:

  • 考虑在moxiecode的tinymce论坛上发布这个问题(这个很具体)

标签: tinymce watir webdriver watir-webdriver


【解决方案1】:

目前,您需要访问并获取底层驱动程序实例。这在 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"

【讨论】:

  • 完美。这正是我认为可能但缺乏/找不到应用知识的答案。期待下一个版本。
  • 几乎完美。如何清除以前的内容? MCE 已填充。焦点在开头,所以发送 "\b" 什么都不做。我未能发送其他控制字符/序列。现在我必须解决预先添加新文本...
  • Wojciech:这适用于我的 Mac - .send_keys [:command, 'a'], :backspace, "hello world" - 在其他平台上,您可能需要 :control 而不是 :command。
  • Wojciech:在 Windows 上,我使用 d.switch_to.active_element.send_keys("\ca") 选择所有文本,然后当我发送新内容时,它会覆盖原来的内容以前有。虽然这与上面 Jarib 的回答完全相同。
【解决方案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/

【讨论】:

    【解决方案3】:

    在最新版本的 TinyMCE 上(尤其是上面示例中使用的 Moxiecode Full Featured 示例中的当前版本),您似乎需要在脚本中添加一个 .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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 2012-03-17
      • 2018-04-01
      • 1970-01-01
      相关资源
      最近更新 更多