如何使用Testcafé与Chrome原生自动化(cdp)测试Chrome通知?
使用Testcafé+Chrome CDP自动化配置通知权限
当然可以自动化设置Chrome的通知权限,以下是三种实用方案,适配不同测试场景:
方案一:用Testcafé内置API直接授权
Testcafé自带setPermissions方法,能精准为当前测试网站授予通知权限,完全跳过授权弹窗。
示例代码:
import { Selector } from 'testcafe'; fixture `Firebase推送通知测试` .page `https://你的测试域名.com` // 每个测试执行前预先授权 .beforeEach(async t => { await t.setPermissions('notifications', 'granted'); }); test('验证推送通知接收', async t => { // 这里编写触发Firebase推送的逻辑(比如调用测试接口发送推送) // 后续可结合CDP命令获取并验证通知内容 });
方案二:通过Chrome启动参数全局允许通知
如果测试环境无需区分域名,可直接给Chrome添加启动参数,默认允许所有网站的通知权限:
示例代码:
fixture `Firebase通知测试` .page `https://你的测试域名.com`; test('测试通知接收流程', async t => { // 测试逻辑 }).browser('chrome --allow-notifications --disable-popup-blocking');
注:--allow-notifications会全局开启通知权限,--disable-popup-blocking确保弹窗不会被拦截,适合快速搭建测试环境。
方案三:调用Chrome CDP命令精细控制
如果需要更底层的权限管理,可直接调用Chrome DevTools Protocol的Browser.grantPermissions命令:
示例代码:
import { ClientFunction } from 'testcafe'; const grantNotificationPerm = ClientFunction(async () => { const cdpSession = await window.chrome.debugger.attach('1.3'); await window.chrome.debugger.sendCommand(cdpSession, 'Browser.grantPermissions', { permissions: ['notifications'], origin: window.location.origin }); await window.chrome.debugger.detach(cdpSession); }); fixture `CDP权限配置测试` .page `https://你的测试域名.com` .beforeEach(async t => { await grantNotificationPerm(); }); test('验证推送通知展示', async t => { // 测试逻辑 });
内容的提问来源于stack exchange,提问作者EdwinR
相关产品推荐
相关产品推荐

