英文:
How to block Google ads from appearing in tests
问题
如何屏蔽测试中出现的Google广告。
为了避免广告,我使用以下方法:
await page.frameLocator('iframe[name="aswift_2"]').frameLocator('iframe[name="ad_iframe"]').getByRole('button', { name: 'Close ad' }).click();
这并不总是有效,所以我还使用了以下方法:
await page.goBack();
await page.goForward();
这在我单独运行每个测试时有效,但当我按顺序运行所有测试时,广告会出现在新的测试位置,导致测试无法通过。
我该如何编写一个脚本来禁用Google广告?
我正在学习在 https://automationexercise.com/ 应用中使用Playwright进行测试。
英文:
How to block Google ads appearing in tests.
To get around the adverts I use:
await page.frameLocator('iframe[name="aswift_2"]').frameLocator('iframe[name="ad_iframe"]').getByRole('button', { name: 'Close ad' }).click();
Which doesn't always work, so I use:
await page.goBack();
await page.goForward();
works as I use each test separately, but when I run all the tests in sequence then ads appear in new test locations and the tests don't pass
How can I write a script to disable Google adverts
I am learning to test in Playwright on the app https://automationexercise.com/
答案1
得分: 1
你可以使用 adblocker-playwright。
或者阻止生成广告的网络资源。请参阅 Playwright 网络文档。
await page.route("**/*", route => {
route.request().url().startsWith("https://googleads.") ?
route.abort() : route.continue();
return;
})
英文:
You can use adblocker-playwright.
Or block network resources that generate Ads. See Playwright network docs
await page.route("**/*", route => {
route.request().url().startsWith("https://googleads.") ?
route.abort() : route.continue();
return;
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论