Remove ads in automated tests

Hi, is there a way to tell my web app not to display fullscreen ads like these?

These ads appear randomly after clicking a link to one of our subpages. Closing these kind of ads through automated testing (locate element and click) appears to be very difficult, because there are many types of ads that can be closed differently.

2 Likes

Can you please share which Selenium supported language you are using for implementation? Sharing two options as per my experience with Selenium:

Recursively Closing Child Windows (or AD Windows)

Is it possible for you to recursively close the windows (using the Window Handles) till the time there is only one window open (which is the parent window)? With such a solution, even if a new AD window(s) pops-up, closing the window(s) will lead you to the Parent Window.

I have written a blog on a similar topic (involving annoying pop-ups) where I have demonstrated how to recursively close the windows (except the Parent Window). Attaching the code (from the above mentioned blog that has implementation in PHP with Selenium).

Automation Testing By Disabling JavaScript (Not highly recommended if you need JS support)

JavaScript is an integral part of web development since it adds dynamism to the website. If you are ok to disable JavaScript for your testing purpose, you can try this option (for disabling annoying ADs).

ChromeOptions and FirefoxOptions provide an option to disable JavaScript support through preferences (example below):

image

image

You can refer to Java example available in the section “How To Disable JavaScript For Automation Testing with Selenium” of this blog for more information.

2 Likes

you can use the route method in Playwright C sharp to block these Google ads during test execution.

Here are the codes

            using var playwright = await Playwright.CreateAsync();
            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
            {
                Headless = false, // Set to false to show the browser UI
            });
            // Create a new page
            var context = await browser.NewContextAsync();
            var page = await context.NewPageAsync();
            await context.RouteAsync("**/*", async r =>
            {
                if (Regex.IsMatch(r.Request.Url, @"googleads"))
                    await r.AbortAsync();
                else
                    await r.ContinueAsync();
            });
            await page.GotoAsync("https://automationexercise.com/");