在柏树和打字稿中设置Tinymce编辑器的内容

发布于 2025-02-10 06:35:28 字数 1115 浏览 2 评论 0原文

我正在使用Typescript构建React JS应用程序。我正在使用柏树为我的应用程序编写集成测试。我还使用打字稿编写柏树测试。我现在正在尝试在柏树测试中设置小型MCE编辑器的内容。我正在尝试使用此库, https://github.com/foreachos/cypress/cypress-tinymce 。但是我不能将该库与打字稿一起使用,因为它仅适用于JavaScript。因此,我查看了设置编辑器值的基础代码。如下。

Cypress.Commands.add('setTinyMceContent', (tinyMceId, content) => {
  cy.window().then((win) => {
    const editor = win.tinymce.editors[tinyMceId];
    editor.setContent(content);
  });
});

因此,我尝试在我的支持/index.js文件中创建该命令的打字稿版本如下。

Cypress.Commands.add('setTinyMceContent', (tinyMceId: string, content: any) => {
    cy.window().then((win) => {
        const editor = win.tinymce.editors[tinyMceId];
        editor.setContent(content);
    })
})

但是它抱怨如下。

Property 'tinymce' does not exist on type 'AUTWindow'.

我该如何修复?

I am building a React Js application using Typescript. I am writing integration tests for my application using Cypress. I am also using Typescript for writing Cypress tests. I am now trying to set the content of the tiny MCE editor in the Cypress test. I am trying to use this library, https://github.com/ForeachOS/cypress-tinymce. But I cannot use that library with TypeScript as it is only for JavaScript. So I had a look at the underlying code that sets the value of the editor. It is as follow.

Cypress.Commands.add('setTinyMceContent', (tinyMceId, content) => {
  cy.window().then((win) => {
    const editor = win.tinymce.editors[tinyMceId];
    editor.setContent(content);
  });
});

So I tried to create the TypeScript version of that command in my support/index.js file as follow.

Cypress.Commands.add('setTinyMceContent', (tinyMceId: string, content: any) => {
    cy.window().then((win) => {
        const editor = win.tinymce.editors[tinyMceId];
        editor.setContent(content);
    })
})

But it is complaining as follow.

Property 'tinymce' does not exist on type 'AUTWindow'.

enter image description here

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2025-02-17 06:35:28

如果您在cypress.d.ts中查看autwindow

/**
  * Window type for Application Under Test(AUT)
*/
type AUTWindow = Window & typeof globalThis & ApplicationWindow

及其下方的声明,用户定义的属性

/**
 * The interface for user-defined properties in Window object under test.
*/
interface ApplicationWindow { } // tslint:disable-line

您可以添加您自己的属性通过声明合并。

在您的/cypress/support/e2e.d.t.ts(cypress v10)或/cypress/support/support/index.d.t.ts(cypress v9

/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable<Subject> {
    setTinyMceContent(tinyMceId: string, content: any): void
  }
}

declare namespace Cypress {
  interface ApplicationWindow { 
    tinymce: any
  } 
}

) =“ https://www.npmjs.com/package/@types/tinymce” rel =“ noreferrer”>@type/tinymce ,这可能很有用。

If you take a look in cypress.d.ts there's a declaration for AUTWindow

/**
  * Window type for Application Under Test(AUT)
*/
type AUTWindow = Window & typeof globalThis & ApplicationWindow

and underneath that is ApplicationWindow for user-defined properties

/**
 * The interface for user-defined properties in Window object under test.
*/
interface ApplicationWindow { } // tslint:disable-line

You can add your own properties via declaration merging.

In your /cypress/support/e2e.d.ts (Cypress v10) or /cypress/support/index.d.ts (Cypress v9)

/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable<Subject> {
    setTinyMceContent(tinyMceId: string, content: any): void
  }
}

declare namespace Cypress {
  interface ApplicationWindow { 
    tinymce: any
  } 
}

There's also @types/tinymce which may be useful down the line.

爱*していゐ 2025-02-17 06:35:28

谢谢@johnydevnull,但是就我而言,我必须进行一些更改,

Cypress.Commands.add('getBySelector', (selector, timeout?, ...args) => {
    return cy.get(`[data-testid=${selector}]`, { timeout, ...args });
});

Cypress.Commands.add('setTinyMceContent', (selector, content) => {
    // wait for tinymce to be loaded
    cy.window().should('have.property', 'tinymce');
    // wait for the editor to be rendered
    cy.getBySelector(selector)
        .find('textarea')
        .as('editorTextarea')
        .should('exist');
    // set the content for the editor by its dynamic id
    cy.window().then((win) =>
        cy.get('@editorTextarea').then((element) => {
            const editorId = element.attr('id');
            const editorInstance = (win as any).tinymce.EditorManager.get().filter((editor) => editor.id === editorId)[0];
            editorInstance.setContent(content);
        })
    );
});

我还准备了命令以阅读柏树步骤的值

Cypress.Commands.add('getTinyMceContent', (selector) => {
    // wait for tinymce to be loaded
    cy.window().should('have.property', 'tinymce');
    // wait for the editor to be rendered
    cy.getBySelector(selector)
        .find('textarea')
        .as('editorTextarea')
        .should('exist');
    // get the content of the editor by its dynamic id
    cy.window().then((win) =>
        cy.get('@editorTextarea').then((element) => {
            const editorId = element.attr('id');
            const editorInstance = (win as any).tinymce.EditorManager.get().filter((editor) => editor.id === editorId)[0];
            const content = editorInstance.getContent();
            return content;
        })
    );
});

,然后您可以检查值:

cy.getTinyMceContent(<selector>).should('contain','abc');

Thanks @JohnyDevNull, but in my case i had to do some changes

Cypress.Commands.add('getBySelector', (selector, timeout?, ...args) => {
    return cy.get(`[data-testid=${selector}]`, { timeout, ...args });
});

Cypress.Commands.add('setTinyMceContent', (selector, content) => {
    // wait for tinymce to be loaded
    cy.window().should('have.property', 'tinymce');
    // wait for the editor to be rendered
    cy.getBySelector(selector)
        .find('textarea')
        .as('editorTextarea')
        .should('exist');
    // set the content for the editor by its dynamic id
    cy.window().then((win) =>
        cy.get('@editorTextarea').then((element) => {
            const editorId = element.attr('id');
            const editorInstance = (win as any).tinymce.EditorManager.get().filter((editor) => editor.id === editorId)[0];
            editorInstance.setContent(content);
        })
    );
});

Also i prepared command for reading a value

Cypress.Commands.add('getTinyMceContent', (selector) => {
    // wait for tinymce to be loaded
    cy.window().should('have.property', 'tinymce');
    // wait for the editor to be rendered
    cy.getBySelector(selector)
        .find('textarea')
        .as('editorTextarea')
        .should('exist');
    // get the content of the editor by its dynamic id
    cy.window().then((win) =>
        cy.get('@editorTextarea').then((element) => {
            const editorId = element.attr('id');
            const editorInstance = (win as any).tinymce.EditorManager.get().filter((editor) => editor.id === editorId)[0];
            const content = editorInstance.getContent();
            return content;
        })
    );
});

in cypress steps then you can check value with:

cy.getTinyMceContent(<selector>).should('contain','abc');
风筝在阴天搁浅。 2025-02-17 06:35:28

在这里,一个强大的解决方案可以处理动态编辑器ID:

Cypress.Commands.add('getBySelector', (selector, timeout?, ...args) => {
  return cy.get(`[data-spec=${selector}]`, { timeout }, ...args);
});

Cypress.Commands.add('setTinyMceContent', (selector, content) => {
  // wait for tinymce to be loaded
  cy.window().should('have.property', 'tinymce');
  // wait for the editor to be rendered
  cy.getBySelector(selector).find('editor textarea').as('editorTextarea').should('exist');
  // set the content for the editor by its dynamic id
  cy.window().then(win =>
    cy.get('@editorTextarea').then(element => {
      const editorId = element.attr('id');
      const editorInstance = (win as any).tinymce.EditorManager.get().filter(editor => editor.id === editorId)[0];
      editorInstance.setContent(content);
    }),
  );
});

Here a robust solution wich handles the dynamic editor ID's:

Cypress.Commands.add('getBySelector', (selector, timeout?, ...args) => {
  return cy.get(`[data-spec=${selector}]`, { timeout }, ...args);
});

Cypress.Commands.add('setTinyMceContent', (selector, content) => {
  // wait for tinymce to be loaded
  cy.window().should('have.property', 'tinymce');
  // wait for the editor to be rendered
  cy.getBySelector(selector).find('editor textarea').as('editorTextarea').should('exist');
  // set the content for the editor by its dynamic id
  cy.window().then(win =>
    cy.get('@editorTextarea').then(element => {
      const editorId = element.attr('id');
      const editorInstance = (win as any).tinymce.EditorManager.get().filter(editor => editor.id === editorId)[0];
      editorInstance.setContent(content);
    }),
  );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文