cypress - 用 Cypress 测试 Monaco Editor

标签 cypress

enter image description here

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

例子:

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

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

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

你好吗全选并更新 使用柏树的 Monaco Editor 中的内容?

编辑:

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

最佳答案

{selectAll}发送 document.execCommand('selectall')这将适用于 contenteditables , textareas , 和 inputs除非自定义代码禁用其默认行为。 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')
  })
})

Cypress 版本:3.3.12020 年更新:
Cypress 版本:5.0.0从编辑器中清除所有文本

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

关于cypress - 用 Cypress 测试 Monaco Editor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56617522/

相关文章:

javascript - Cypress - 如何在 iframe 中的元素之间切换

javascript - 在 visual studio 代码中丢失了 cypress 的智能感知

javascript - 如何通过数组比较两个元素的文本内容

automation - 无法使用 Cypress 在 CK 编辑器中输入数据

javascript - 如何跳过基于 IF ELSE 语句的 Cypress 测试?

javascript - Cypress.io 如何处理异步代码

javascript - 如何从 div 类 Cypress 中获取文本

javascript - TypeError : selector. 拆分不是 Cypress 的函数

console - Cypress 运行命令而无需重新运行测试

javascript - 访问其父项被隐藏的元素 - cypress.io