我想使用 TestCafe 来检查我的 Service Worker 是否注册成功。我可以使用以下代码从浏览器控制台进行检查
(await window.navigator.serviceWorker.getRegistration()).active.scriptURL === 'PATH TO SERVICE WORK JS FILE'
尝试从 Test Cafe 运行此代码时
ClientFunction
,导航器对象为空。
如何检查 Service Worker 是否已加载?
import { ClientFunction } from 'testcafe';
const getServiceWorker= ClientFunction(() => window.navigator.serviceWorker.getRegistration());
fixture `My fixture`
.page `http://www.example.com/`;
test('My Test', async t => {
const serviceWorker = await getServiceWorker();
await t
.expect(serviceWorker && serviceWorker !== null && serviceWorker.active).ok();
});
编辑: 我在 TestCafe Github 上创建了一个问题 https://github.com/DevExpress/testcafe/issues/7239
从客户端函数返回要测试的最终值:
import { ClientFunction } from 'testcafe';
const getScriptURL = ClientFunction(() => {
return window.navigator.serviceWorker.getRegistration()
.then(serviceWorker => serviceWorker?.active?.scriptURL);
});
fixture `My fixture`
.page `http://www.example.com/`;
test('My Test', async t => {
const scriptURL = await getScriptURL();
await t.expect(scriptURL).eql('PATH TO SERVICE WORK JS FILE');
});
如果您仍然得到与预期不同的值,请在调试模式下检查浏览器控制台:
import { Selector } from 'testcafe';
fixture `New Fixture`
.page `about:blank`;
test(`New Test`, async t => {
await t.debug();
});
如果
value(await window.navigator.serviceWorker.getRegistration()).active.scriptURL
与您在没有 TestCafe 的应用程序中看到的内容不匹配,那么它看起来像是一个错误。在这种情况下,请在 TestCafe 存储库中创建一个问题,并附加一个重现此行为的示例。