TestCafe自动化测试中TinyMCE编辑器无法加载问题求助
解决TestCafe无法加载TinyMCE编辑器的问题
以下是针对该问题的具体解决步骤:
替换硬编码等待为智能等待
硬编码的wait()完全依赖固定时长,可靠性极低,建议换成TestCafe内置的元素等待机制,确保编辑器相关元素加载完成后再继续操作。比如等待TinyMCE的iframe容器出现:const tinyMceIframe = Selector('iframe').withAttribute('src', /tinymce/); await t.waitForSelector(tinyMceIframe, { timeout: 15000 });处理TinyMCE的动态iframe加载
TinyMCE编辑器的内容通常在独立iframe里,TestCafe需要先切换到这个iframe上下文才能识别内部元素。先等待iframe加载完成,再切换上下文:const editorIframe = Selector('#tinymce iframe'); // 替换为你页面中TinyMCE的iframe选择器 await t.waitForSelector(editorIframe); // 切换到iframe上下文 const editorBody = Selector('body').with({ boundTestRun: t }).parent(editorIframe);禁用页面缓存
页面缓存可能导致TinyMCE的脚本资源加载异常,在fixture里添加缓存禁用配置:fixture`test` .page('https://example.com') .disablePageCaching;检查浏览器环境
确认测试用的浏览器没有启用阻止第三方脚本的隐私设置,部分浏览器的严格隐私模式会拦截TinyMCE的CDN资源。可以尝试切换到其他浏览器测试,比如Chrome换Firefox,反之亦然。修改后的示例代码
把原来的硬编码等待全部替换为智能等待,示例如下:import { screen, within } from "@testing-library/testcafe"; import { Selector } from "testcafe"; fixture`test` .page('https://example.com') .disablePageCaching; test('create element', async t => { // 等待按钮出现并点击 const text1 = Selector(".MuiBox-root.css-ircbac > button"); await t.waitForSelector(text1, { timeout: 10000 }) .click(text1); // 等待TinyMCE编辑器的iframe加载 const tinyMceIframe = Selector('iframe').withAttribute('src', /tinymce/); await t.waitForSelector(tinyMceIframe, { timeout: 15000 }); // 切换到iframe操作编辑器内容 const editorBody = Selector('body').with({ boundTestRun: t }).parent(tinyMceIframe); await t.typeText(editorBody, '测试内容'); });
内容的提问来源于stack exchange,提问作者Sivaram
相关产品推荐
相关产品推荐

