无法使用量角器获取摩纳哥编辑器中的文本 [英] Unable to get the text inside the monaco editor using protractor

查看:62
本文介绍了无法使用量角器获取摩纳哥编辑器中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 protractor-cucumber 框架为 Monaco-editor 编写 BDD 测试.不久前,我发现了 Monaco API 以及如何以编程方式在编辑器中设置值.但这一次,我无法使用量角器在 monaco 编辑器中获取文本.这是我的代码示例:

I am writing BDD tests for Monaco-editor using the protractor-cucumber framework. A while ago, I found about Monaco API's and how to set the value inside the editor programmatically. But this time, I am unable to get the text inside monaco editor using protractor. Here's a sample of my code:

browser.ignoreSynchronization = true;
        let driver = browser.driver;
        // iframe ids

       let iframeID = 'editorFrame';

       let editorSpanXpath = '//div[@id="editorContainer"]//div[contains(@class, "monaco- editor")]//div[contains(@class, "editor-scrollable")]'
      // switching to the iFrame to perform tasks inside it
        browser.switchTo().frame(iframeID);

    // clicking on a div inside the editor to ascertain that
    // the browser knows where to  run the script

        driver.findElement(by.xpath(editorSpanXPath)).click();
         browser.executeScript('this.monaco.editor.getModels()[0].getValue()').then(function(editorText){
               let replaceString = 'abracadbra' + editorText
               browser.executeScript('this.monaco.editor.getModels()[0].setValue("' + replaceString + '")');
        }
        );

这里的问题是 'editorText' 的值一直为空.在运行我的 BDD 测试时,编辑器中的值被替换为 'abracadabranull'

The problem here is that the value of 'editorText' keeps coming up null. On running my BDD tests, the value inside the editor is replaced by 'abracadabranull'

编辑器使用一些默认文本进行初始化.由于setValue"函数正在运行,我认为浏览器驱动程序在获取加载编辑器的 iFrame 方面没有问题.

The editor is initialized with some default text. And since the 'setValue' function is working, I figure that the browser driver has no trouble in getting the iFrame where the editor gets loaded.

任何帮助将不胜感激.

推荐答案

最后,是一个简单的 return 语句挽救了这一天:

In the end, it was a simple return statement that saved the day:

browser.ignoreSynchronization = true;
    let driver = browser.driver;
    // iframe ids

   let iframeID = 'editorFrame';

   let editorSpanXpath = '//div[@id="editorContainer"]//div[contains(@class, "monaco- editor")]//div[contains(@class, "editor-scrollable")]'
  // switching to the iFrame to perform tasks inside it
    browser.switchTo().frame(iframeID);

// clicking on a div inside the editor to ascertain that
// the browser knows where to  run the script

    driver.findElement(by.xpath(editorSpanXPath)).click();
     browser.executeScript('return this.monaco.editor.getModels()[0].getValue()').then(function(editorText){
           let replaceString = 'abracadbra' + editorText
           browser.executeScript(' return this.monaco.editor.getModels()[0].setValue("' + replaceString + '")');
    }
    );

非常感谢这个 为我指明正确方向的答案.

Many thanks to this answer for pointing me in the right direction.

这篇关于无法使用量角器获取摩纳哥编辑器中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆