【问题标题】:Inserting text in Ace Editor via Protractor throws error通过量角器在 Ace 编辑器中插入文本会引发错误
【发布时间】:2017-12-20 06:27:19
【问题描述】:

我正在尝试运行自动化测试,测试用例的步骤之一是清除 Ace 编辑器并在其中插入文本。

之前我有常规的内容可编辑 DIV,我可以在其中插入文本,但在切换到 Ace Editor 后就不行了。

我所做的只是在元素上调用“clear”方法,然后在其上调用“sendText”。 我尝试在清除之前添加“单击”方法,以确保 Ace Editor 确实具有焦点,并且确实将焦点带到了 Ace Editor,但它仍然会引发错误,即元素应该是用户可编辑的以清除它。

我还尝试在清除它之前将其休眠 5s,以防万一,在 Ace 使 div 可内容编辑之前触发清除。

这里是准确的错误。

Failed: invalid element state: Element must be user-editable in order to clear it.

【问题讨论】:

    标签: protractor ace-editor


    【解决方案1】:

    棘手的问题。 由于 Ace Editor 不是本机浏览器元素,因此我查找了它们提供的 api。好像你可以使用.selection.selectAll().removeLines()

    如果我是你 - 我会尝试调用 JS 函数,该函数会选择所有内容并调用 .removeLines()

    await browser.executeScript(`editor_reference.selection.selectAll(); editor_reference.removeLines()`) // variable will be diferent, depending how you connected editor object
    

    http://www.protractortest.org/#/api?view=webdriver.WebDriver.prototype.executeScript

    https://ace.c9.io/#nav=api&api=editor

    您可以在他们的示例页面 (https://ace.c9.io/) 上看到它是如何工作的 - 在浏览器控制台中执行此操作,编辑器应该被清除:

    editor.selection.selectAll(); editor.removeLines()
    

    【讨论】:

      【解决方案2】:

      在输入之前发送ctrl-a键(cmd-a在mac上)以全选,这样就不需要清除

      【讨论】:

        【解决方案3】:

        我以前没有使用 Ace 编辑器的经验。我使用来自https://ace.c9.io/build/kitchen-sink.html 的演示 ace 编辑器为您面临的问题找到解决方案。

        我在演示 Ace 编辑器中测试了以下代码,它工作正常。不确定 ace 编辑器是如何在您的应用程序中实现的。您可以在您的应用程序中尝试并提供您的想法。

        browser.actions().mouseMove($(".ace_active-line")).click()
        .sendKeys(protractor.Key.chord(protractor.Key.COMMAND, "a"))
        .sendKeys(protractor.Key.BACK_SPACE)
        .sendKeys("content in line1\ncontent in line2")
        .perform()
        

        如果您使用的是 Windows 机器,请使用 protractor.Key.CONTROL 而不是 protractor.Key.COMMAND

        希望一切顺利!

        【讨论】:

          猜你喜欢
          • 2013-09-16
          • 1970-01-01
          • 2015-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-01
          • 1970-01-01
          • 2014-12-13
          相关资源
          最近更新 更多