How to handle advertisement which appears randomly on any screen

Hey,

I am facing an issue while automating native app and wap(mobile web) that Advertisement appears randomly on any screen and my scripts get failed due to advertisement screen.
This Advertisement has close(x) button at the top left corner. We can close it.

Please give your suggestions, How can we handle this Advertisement which appears randomly on any screen?

Below are two points come in my mind to handle this scenario:

  1. I will check Advertisement appears or not at every step (after click or other action) but it increases my execution time.
  2. Is there any way, we keep monitoring that Advertisement appears or not. If Advertisement appears just close the Advertisement if not then keep execute the steps of scripts.

Please suggest any workaround to handle such Advertisement so that our scripts do not fail due to Advertisement.

Thanks in advance!!

What is the timing of the advertisements? Do they show up when the screen first comes up, or can they show up even while you are idle on the screen? If the former, you can use the factory design pattern to detect the screen you are on, and if it detects the advertisement, dismiss the advertisement and try again. If the latter, you could (sigh) wrap all actions in a try/catch block to check for MissingElement (or the equivalent exception) and if the exception is caught, check for advertisement, dismiss and retry if present.

@willosser Thanks for your reply…

Advertisements would display around 10-20 seconds on the screen.
It can come at any situation for example : when user lands on any screen then it may come or when user idle on the screen for some time then it may come. We can’t predict when advertisement may come.

Regarding “factory design pattern to detect the screen you are on, and if it detects the advertisement, dismiss the advertisement and try again. If the latter, you could (sigh) wrap all actions in a try/catch block to check for MissingElement (or the equivalent exception) and if the exception is caught, check for advertisement, dismiss and retry if present.”. Could you please give me some code for example regarding this approach.

Thanks very much!!

@Chirag_Singh,

Looks like it’s the latter case. Actions on screens are, in general either check for something or act upon it. Given this, you can write a wrapper to either check for the advert beforehand, or dismiss it if it showed up. The latter has a smaller window of failure since the advert can show up right after you checked for it’s existence

Since you didn’t specify a language, I’ll write in pseudo code to (hopefully) avoid language translation issues.

def protected_action(function, args)
  try
    eval function args
  catch MissingElement
    if advert_present
      dismiss_advert
      eval function args
    else
      reraise exception
    end
  end
end

@willosser
Sorry for late reply, I am using Java with appium. If you don’t mind could you please provide some code in java to handle such scenario. Still I don’t have clear picture how can we handle it?

Thanks,

Sorry, Chirag, but we are not using Java, so I have nothing to show you.

@Priyank_Shah

Could you please have a look int it, whenever you will get time.

Thanks very much!!

Can u please install adblock plus app to ur device. and check… i m not sure will it work for mobileweb.

willossers pseudo code as a java example for clicking a button using annotations and the page object pattern (you could use normal find by methods instead)

@AndroidFindBy(id="your_id")
private MobileElement button;

@AndroidFindBy(id="your_ad_close_button_id")
private MobileElement ad_close;

    public void protected_click(MobileElement element){ //you can call this function with any mobileElement (e.g. your button); this could also return another page object and not void.
        try{
//you may want to wait here until the element is visible or clickable using wait.until(ExpectedConditions.elementToBeClickable(element)); or similar (add a timeout exception in this case as well)
            element.click();
        }
        catch (NoSuchElementException e){ //element could not be found because it is covered by the ad
            if (ad_close.isDisplayed()){ //this throws an element not found exception so I skipped rethrowing.. otherwise this has to be in another try catch block or handled differently
                ad_close.click();
                //now it should be possible to click the element
                element.click();
            }
        }

not tested, but you should get the idea

2 Likes

@rompic

Thanks for your help, now I will write the code :slight_smile:

@rompic : in my case random advertisement do not come on any click.
It comes randomly.
However I handled this issue in android but not sure how to handle this with IOS…

how did you handle it in android?

Basically I create one class for Thread using java.
Whenever my test case get started , thread starts running in parallel and check for current activity running on app
hence whenever running thread found the activity of random ads, it removes them

sorry no clue… found https://github.com/appium/java-client/issues/87 but you already asked there.

is there a reoccuring element in this notification that could be identified?

Hi Argneshu, would you mind if you share the code ?

Thanks,

Hey, i am facing a similar problem, I cant able to get the element of the ‘click to continue’ portion in the ads, so i cant get the id or anything to locate it. do you have any idea how to deal this?

Please start a new thread, very little here suggests that your problem is the same as the original problem @dheeerazzz . That way you can share logs and screenshots and code samples without bothering people from the old thread.