Appium UiWatchers 监听解决各种非期待弹窗,弹层,弹弹弹等问题

hello · April 13, 2018 · Last by hello replied at January 21, 2019 · 3620 hits

app 自动化时,各种不期待的弹层弹窗,升级广告等时有飞出,由于弹窗具有不定时,不定页面等很多不确定性。有的弹窗很不友好,不×掉,很难进行下一步操作,造成 测试用例失败。而判断是否有弹窗,弹层很麻烦。
研究一下 appium 和手机通信的原理就不难发现,运行 appium 时推送手机 AppiumBootstrap.jar 的中,有这么一段代码再listenForever

/**
 * The Bootstrap class runs the socket server.
 * 
 */
public class Bootstrap extends UiAutomatorTestCase {

  public void testRunServer() {
    Find.params = getParams();
    final boolean disableAndroidWatchers = Boolean.parseBoolean(getParams()
        .getString("disableAndroidWatchers"));
    final boolean acceptSSLCerts = Boolean.parseBoolean(getParams().getString(
        "acceptSslCerts"));

    SocketServer server;
    try {
      server = new SocketServer(4724);
      server.listenForever(disableAndroidWatchers, acceptSSLCerts);
    } catch (final SocketServerException e) {
      Logger.error(e.getError());
      System.exit(1);
    }

  }
}

那么我们可以利用这个长监听干点点掉弹窗的事儿会不会很爽弹窗一弹出就把他点掉点掉点掉
其实在UiWatchers.java中作者已经写了很多UI监听了贴一小段代码
public class UiWatchers {
  private static final String LOG_TAG = UiWatchers.class.getSimpleName();
  private final List<String>  mErrors = new ArrayList<String>();

  /**
   * We can use the UiDevice registerWatcher to register a small script to be
   * executed when the framework is waiting for a control to appear. Waiting may
   * be the cause of an unexpected dialog on the screen and it is the time when
   * the framework runs the registered watchers. This is a sample watcher
   * looking for ANR and crashes. it closes it and moves on. You should create
   * your own watchers and handle error logging properly for your type of tests.
   */
  public void registerAnrAndCrashWatchers() {

    UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {
      @Override
      public boolean checkForCondition() {
        UiObject window = new UiObject(new UiSelector()
            .className("com.android.server.am.AppNotRespondingDialog"));
        String errorText = null;
        if (window.exists()) {
          try {
            errorText = window.getText();
          } catch (UiObjectNotFoundException e) {
            Log.e(LOG_TAG, "dialog gone?", e);
          }
          onAnrDetected(errorText);
          postHandler();
          return true; // triggered
        }
        return false; // no trigger
      }
    });

在这个类中我们加入自己的弹窗监听
  public void registerMyPopupWatcher() {
        UiDevice.getInstance().registerWatcher("myPopup", new UiWatcher() {
          @Override
          public boolean checkForCondition() {

            //广告提示
            UiObject ggPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/close_view"));

            //站点切换
            UiObject addChgPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/bt_cancel"));

            //升级提示
            UiObject upPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/close"));

            if (upPop.exists()) {
                System.out.println("you have a updateApp popup window");
                try {
                    upPop.clickAndWaitForNewWindow();
                    return true; // triggered
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "Exception", e);
                }
            }else if (ggPop.exists()) {
                System.out.println("you have a popup window");
                try {
                    ggPop.clickAndWaitForNewWindow();
                    return true; // triggered
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "Exception", e);
                }
            }else if (addChgPop.exists()) {
                System.out.println("you have a change site popup window");
                try {
                    addChgPop.clickAndWaitForNewWindow();
                    return true; // triggered
                } catch (UiObjectNotFoundException e) {
                    Log.e(LOG_TAG, "Exception", e);
                }
            }
            return false; // no trigger
          }
        });

        Log.i(LOG_TAG, "Registered myPopupPopup Watchers");
      }
在listenForever的方法中去调用一下这样我们也监听Forever了

写完编译成AppiumBootstrap.jar包放在\Appium\node_modules\appium\build\android_bootstrap\下面随着appium服务启动时推送到手机中
跑一下看效果
app中弹窗中有如下resourceId的统统被点掉了
resourceId="com.gift.android:id/close_view";
resourceId="com.gift.android:id/bt_cancel";
resourceId="com.gift.android:id/close";
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 22 条回复 时间 点赞

这个修改对 uiautomator2 有效吗?

hello #3 · April 13, 2018 Author
雨夜狂奔 回复

uiautomator2 没试,UI2 应该也有类似的方法吧。你研究一下

你这效果图被吃了

hello #5 · April 13, 2018 Author
spring-ssh 回复

哈哈哈,抱歉啊、不知道为啥贴图没有贴上

ua1 和 ua2 里面都有对应的实现,不过他们写死了很多代码,应该是通过读取 capability 里面的参数动态设置的。

hello #7 · April 16, 2018 Author

capability 具体配置方式没有研究,发现代码里面有,改代码也蛮方便的

@success 干货! 谢谢 比较通用了.

hello #9 · April 16, 2018 Author
老马 回复

谢谢小马哥。

hello #10 · April 16, 2018 Author

动态配置的方式,我再研究研究。能动态配置确实不错,代码写死了的话,就有点不灵活了。多谢给予的思路点拨

hello 回复

回头咱们社区成立以 appium 的改进开源团队吧,一起研究下如何改进。

hello #12 · April 16, 2018 Author

不错不错,期待中。

io.appium.java_client.events.api.general.AppiumWebDriverEventListener api 其实有提供相关的监听器的

hello #14 · April 16, 2018 Author
战 神 回复

多谢大神。这 api 我好好学学😙

楼主,你好

你的方法是,修改 AppiumBootstrap.jar 达到监控弹出框的效果。
如果不修改这个 jar,Appium 服务器能监控到手机报错了吗?

hello #16 · May 08, 2018 Author
george 回复

不修改这个 jar,就要自己在手机里部署个服务开启个监听,来做类似的事情了把?感觉这样得不偿失啊。

感觉 Appium 既然能监听到手机端的报错,应该有比较简便的方法可以把监听到的报错信息保存下来吧

不过我找了很久,没有找到好方法。

所以我最后用了一个很笨的放方法,用 adb monitor 监控报错。

hello #18 · May 11, 2018 Author
george 回复

Appium 其实也是和这个 appiumbootstrap 交互的。保存信息的话。可以写 log 的

hello 回复

capability 里面的参数动态设置的方法,请问楼主有木有思路,能否指点下。由于我用的是 uiautomator2,你这方法对我木有效。

macaca 有相关的监听 API 吗

hello #22 · January 21, 2019 Author
bingyan719 回复

uiautomator2 我没有研究过,不好意思

hello #23 · January 21, 2019 Author

不知道哎,你研究一下看看

hello 2018 年 度个人测开账单 中提及了此贴 25 Jan 18:37
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up