将 UIAutomator 与 Admob Cookie 同意书一起使用

Use UIAutomator with Admob Cookie Consent Form

当 运行 测试时,EU Cookie Consent Form 不断弹出,提供以下选项:

如何使用 UIAutomator 访问这些按钮以便继续?

可以将这些按钮作为 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);
}