【问题标题】:Testing monaco-editor with Cypress使用 Cypress 测试 monaco-editor
【发布时间】:2019-06-16 09:13:30
【问题描述】:

上面是 Monaco Editor 渲染的 dom 节点的层次结构。渲染了一个textarea 节点,但不允许修改现有内容

例子:

如果编辑器中的内容是“Foo”,那么这段代码……

cy.get('.react-monaco-editor-container textarea')
      .type('{selectall}')
      .type('blah');

...只会在编辑器中添加 blah,导致 "blahFoo"

如何使用 cypress 在 monaco 编辑器中全选并更新内容?

编辑:

到目前为止,我已经尝试了所有建议:.click()、.clear() 等。它不起作用。请仅在您尝试过并且有效的情况下提供建议。

【问题讨论】:

    标签: cypress


    【解决方案1】:

    {selectAll} 发送document.execCommand('selectall'),它将适用于contenteditablestextareasinputs,除非自定义代码禁用其默认行为。 monaco-editor 似乎就是这种情况。

    我能够使用 {ctrl}a 替换整个选择:

    /// <reference types="cypress" />
    
    describe('monaco', ()=>{
      it('can type', ()=>{
        cy.visit('https://microsoft.github.io/monaco-editor/index.html')
        cy.get('#editor')
        .click()
        // change subject to currently focused element
        .focused()
        .type('{ctrl}a')
        .type('foobar') 
      })
    })
    

    另外,这是一个测试ctrl+f 功能的示例:

    describe('monaco', ()=>{
      it('can type', ()=>{
        cy.visit('https://microsoft.github.io/monaco-editor/index.html')
        cy.get('.monaco-editor textarea:first')
        .type('{ctrl}f')
        .focused()
        // find the word 'canvas'
        .type('canvas')
      })
    })
    
    

    赛普拉斯版本:3.3.1

    2020 年更新:

    赛普拉斯版本:5.0.0

    从编辑器中清除所有文本

    
    cy.get( '.monaco-editor textarea:first' ).click().focused().type( '{ctrl}a' ).clear();
    
    

    【讨论】:

    • 感谢您的回答。我试过这个,它似乎没有替换编辑器的内容。它只输入“foobar”,其余的编辑器内容保持不变。
    • 啊,因为我用的是mac。你用的是windows对吗?
    • 我在 Ubuntu 上,你可以试试 {cmd}a
    • 是的,它有效,建议更新您的答案
    • 我在 Mac 上本地运行 cypress,在 Linux 上运行 CI。为了使全选技巧在两个平台上都起作用,我使用const selectAllKeys = Cypress.platform == 'darwin' ? '{cmd}a' : '{ctrl}a';cy.get('.monaco-editor textarea:first').type(selectAllKeys)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多