如何制作一个合适的UiWatcher私有类()

时间:2014-05-09 20:56:25

标签: java android automation uiautomator

我正在用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);
}

2 个答案:

答案 0 :(得分:0)

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

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

然后将crash声明为:

UiWatcher crash = new MyUIWatcher();

答案 1 :(得分:0)

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