【问题标题】:How to make a proper Private class of UiWatcher()如何制作合适的 UiWatcher() 私有类
【发布时间】:2014-06-27 17:42:26
【问题描述】:

我正在使用 UiWatcher 编写崩溃代码,但我的团队负责人评论说他需要一个“不是匿名类。一个适当的私有类。”那么我如何使用 UiWatcher() 来做到这一点

private void CrashWatcher() {
    UiWatcher crash = new UiWatcher() {

//我的领导评论:不是匿名类。一个适当的私人课程。

            public boolean checkForCondition() {
                UiObject crashButton = new UiObject(
                         new UiSelector().textStartsWith("Unfortunately,"));
            if (crashButton.exists()) {
                log("Found the OK dialog");
                UiObject okButton = new UiObject(new UiSelector()
                        .className("android.widget.Button").text("OK"));
                try {
                    okButton.click();
                } catch (UiObjectNotFoundException e) {
                    log("The chance of not having 'OK' button when the application crash is extremely less.");
                    return false;
                }
            }
            return true;
        }

    };

    // Register watcher
    UiDevice.getInstance().registerWatcher(CRASH_WATCHER_NAME, crash);
}

【问题讨论】:

    标签: java android automation android-uiautomator


    【解决方案1】:

    因此,在命名的内部类中扩展 UIWatcher(包含在您现有的类文件/类主体中):

    public class MyUIWatcher implements UiWatcher() {
        public boolean checkForCondition() {
        // logic
        }
    }
    

    然后将crash 声明为:

    UiWatcher crash = new MyUIWatcher();
    

    【讨论】:

    • 在这样做时出错,即使我试图扩展它,但它给出了错误。
    • 本地类 MyUIWatcher 的非法修饰符;只允许抽象或最终
    • @Dummycoder 那么你并没有告诉我所有的细节。让它成为一个内部类,并实例化它。
    • @Dummycoder 这正是我在回答中写的。
    【解决方案2】:
    public static class MyUIWatcher implements UiWatcher {
        public boolean checkForCondition() {
            UiObject crashButton = new UiObject(
                    new UiSelector().textStartsWith("Unfortunately,"));
            if (crashButton.exists()) {
                log("Found the OK dialog");
                UiObject okButton = new UiObject(new UiSelector().className(
                        "android.widget.Button").text("OK"));
                try {
                    okButton.click();
                } catch (UiObjectNotFoundException e) {
                    log("The chance of not having 'OK' button when the application crash is extremely less.");
                    return false;
                }
            }
            return true;
        }
    
    };
    
    // Define watcher
    private void CrashWatcher() {
    
        UiWatcher crash = new MyUIWatcher();
        // Register watcher
        UiDevice.getInstance().registerWatcher(CRASH_WATCHER_NAME, crash);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2011-08-26
      • 2020-12-14
      相关资源
      最近更新 更多