如何阻止Google广告在测试中出现

huangapple go评论126阅读模式
英文:

How to block Google ads from appearing in tests

问题

如何屏蔽测试中出现的Google广告。

为了避免广告,我使用以下方法:

  1. await page.frameLocator('iframe[name="aswift_2"]').frameLocator('iframe[name="ad_iframe"]').getByRole('button', { name: 'Close ad' }).click();

这并不总是有效,所以我还使用了以下方法:

  1. await page.goBack();
  2. 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 网络文档

  1. await page.route("**/*", route => {
  2. route.request().url().startsWith("https://googleads.") ?
  3. route.abort() : route.continue();
  4. return;
  5. })
英文:

You can use adblocker-playwright.

Or block network resources that generate Ads. See Playwright network docs

  1. await page.route("**/*", route => {
  2. route.request().url().startsWith("https://googleads.") ?
  3. route.abort() : route.continue();
  4. return;
  5. })

huangapple
  • 本文由 发表于 2023年6月22日 01:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定