testing - 在 TestCafe 中等待 'WebComponentsReady' 事件的最佳方法是什么?

标签 testing web-component testcafe testcase browser-testing

我想在运行任何 TestCafe 测试之前等待页面中的 Web 组件升级(换句话说,在运行测试之前等待 WebComponentsReady 事件)。执行此操作的最佳方法是什么?

最佳答案

TestCafe 在 DOMContentReady 事件引发后开始在页面上执行任何操作。如我所见,可以在 DOMContentReady 之前引发 WebComponentsReady 事件。 TestCafe 允许您使用 ClientFunction 在浏览器中等待一些事件:

const waitForWebComponentsReady = ClientFunction(() => {
    return new Promise(resolve => {
        window.addEventListener('WebComponentsReady', resolve);
    });
});

await waitForWebComponentsReady();

但是,请注意 TestCafe 不能保证这段代码会在 WebComponentReady 事件引发之前执行。因此,这个 Promise 不会被解决。

作为解决方案,您可以找到另一种方法来确定是否加载了所需的 Web 组件。例如,您可以检查某些元素在页面上是否可见:

await t.expect(Selector('some-element').visible).ok();

与此同时,TestCafe 对add the capability to execute a custom script before page initialization scripts 提出了一个功能建议.实现该功能后,您将能够使用如下代码:

import { ClientFunction } from 'testcafe';

const initScript = `window.addEventListener('WebComponentsReady', () => window.WebComponentsLoaded = true);`;

fixture `My Fixture`
    .page `http://example.com`
    .addScriptToEachPage(initScript)
    .beforeEach(async t => {
        await t.expect(ClientFunction(() => window.WebComponentsLoaded)()).ok();
    });

关于testing - 在 TestCafe 中等待 'WebComponentsReady' 事件的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46066980/

相关文章:

testing - 检查 img src 在 TestCafe 中的路径是否正确

testing - Web辅助功能测试工具

java - 使用 Ant 运行 JUnit 测试

testing - 如何使用jmeter执行存储过程并得到结果

vue.js - 为什么我的对象和数组 Prop 变成子组件中的字符串?

docker - Chromium 使用 TestCafe Docker 镜像挂起

testing - 低内存开销 XML 解析

html - 规范中不允许在自定义元素中使用自关闭标签的原因是什么?

javascript - 8Tracks API 自定义元素与 Polymer 不起作用

api - 我们可以使用 testCafe 自动化 API 吗?