androidadmobandroid-uiautomatoradmob-cookieconsent

Use UIAutomator with Admob Cookie Consent Form


When running tests, the EU Cookie Consent Form keeps popping up, providing these options:

How to access these buttons with the UIAutomator, in order to continue?


Solution

  • One can get these buttons as an UiCollection:

    @Test
    public void CookieConsent() {
    
        UiSelector selector = getSelector("buttons");
        UiCollection elements = new UiCollection(selector);
        UiObject buttonYes, buttonNo, buttonAdFree;
    
        try {
            if (elements.getChildCount() == 3) {
    
                // Yes, continue to see relevant ads
                buttonYes = elements.getChild(getSelector("btn0"));
                assertNotNull(buttonYes);
    
                // No, see ads that are less relevant
                buttonNo = elements.getChild(getSelector("btn1"));
                assertNotNull(buttonNo);
    
                // Pay for the ad-free version
                buttonAdFree = elements.getChild(getSelector("btn2"));
                assertNotNull(buttonAdFree);
    
                /* click "yes" */
                assertTrue(buttonYes.isClickable());
                buttonYes.click();
            }
    
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    private UiSelector getSelector(String resourceId) {
        return new UiSelector()
          .className(android.view.View.class.getName())
          .resourceId(resourceId);
    }