Use UIAutomator with Admob Cookie Consent Form

86 views Asked by At

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

  • Yes, continue to see relevant ads

  • No, see ads that are less relevant

  • Pay for the ad-free version

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

1

There are 1 answers

0
Martin Zeitler On BEST ANSWER

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);
}