如何处理Chrome共享剪贴板授权弹窗以实现自动化复制粘贴?
解决Chrome剪贴板授权弹窗阻断TestCafe测试的问题
针对你遇到的Chrome剪贴板授权弹窗阻断复制粘贴测试的问题,有两种可靠的解决方式:
方式一:通过Chrome启动参数预先授予权限
直接给Chrome添加命令行参数,指定目标网站的剪贴板权限,跳过授权弹窗。
命令行启动时配置
运行TestCafe时,给Chrome添加权限策略参数:
testcafe chrome:headless --args="--permissions-policy=clipboard-read=https://www.chegg.com,clipboard-write=https://www.chegg.com" 你的测试文件路径.js
如果需要更宽松的权限(仅测试场景推荐),可以用通配符允许所有网站:
testcafe chrome:headless --args="--permissions-policy=clipboard-read=*,clipboard-write=*" 你的测试文件路径.js
编程式配置(TestCafe Runner API)
如果用TestCafe的编程API启动测试,可在指定浏览器时附加参数:
const createTestCafe = require('testcafe'); let testcafe = null; createTestCafe('localhost', 1337, 1338) .then(tc => { testcafe = tc; const runner = testcafe.createRunner(); return runner .src('你的测试文件路径.js') .browsers('chrome:headless --args="--permissions-policy=clipboard-read=https://www.chegg.com,clipboard-write=https://www.chegg.com"') .run(); }) .then(failedCount => { console.log('失败测试数: ' + failedCount); testcafe.close(); });
方式二:使用TestCafe内置权限API
TestCafe 1.14.0及以上版本支持直接在测试代码中设置浏览器权限,无需修改启动参数:
修改你的测试代码,在beforeEach中预先授予剪贴板权限:
import { Selector } from 'testcafe'; const editor = Selector('div[class="full-bleed-uic-editor editor editable"]'); fixture`copy and paste` .meta({ fixtureID: 'f-0001', author: '', app: '', }) .beforeEach(async (t) => { console.log('Copy and paste'); // 为目标网站授予剪贴板读写权限 await t.setPermissions({ clipboard: 'granted' }, 'https://www.chegg.com'); }); test.meta({ priority: 'p1', testID: 't-0001', testType: 'Smoke', testRailCaseId: '', })('Copy and paste', async (t) => { await t.navigateTo('https://www.chegg.com'); await t.click(editor); await t.typeText(editor, 'Python is very easy programming language'); await t.pressKey('ctrl+a ctrl+c ctrl+v ctrl+v'); });
备选方案:禁用剪贴板权限提示弹窗
如果上述方法无效,可尝试用Chrome标志直接禁用该提示:
testcafe chrome --args="--disable-features=ClipboardPermissionPrompt" 你的测试文件路径.js
内容的提问来源于stack exchange,提问作者user2958388
相关产品推荐
相关产品推荐

