简体   繁体   English

ADB Shell获取Toast消息

[英]ADB shell get Toast messages

I have been working around a Java test framework and ended up with testing an android application which does a lot of toast messages. 我一直在研究Java测试框架,最后测试了一个执行很多Toast消息的android应用程序。

The problem is the framework doesn't use anything other than ADB and I can't use any extra libs. 问题在于该框架除了ADB之外不使用任何其他东西,并且我不能使用任何额外的库。

so Is there any way to get the toasted message from an application via adb? 所以有没有办法通过adb从应用程序中获取消息? I wouldn't mind writing hundreds of lines for this alone.. 我不介意为此写数百行。

You can check toast messages through events with adb shell uiautomator events from the command line. 您可以adb shell uiautomator events通过带有adb shell uiautomator events检查Toast消息。 Within your java test framework, however, you can create a method for code reuse that runs this command and parses it for whatever string you want to compare it to with a try block as such: 但是,在Java测试框架中,您可以创建一种代码重用方法,该方法运行此命令,并使用try块将其与要比较的字符串进行解析,例如:

try {
        Process process = Runtime.getRuntime().exec("uiautomator events");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        StringBuilder log=new StringBuilder();
        String toastMessage;
        while ((toastMessage = bufferedReader.readLine()) != null) {
            log.append(toastMessage).append("\n");

        if (toastMessage.contains(myToastStringVar)) {
              Log.i("ToastMessageMatcher", "" // <-- your info message);
            } else {
              Log.e("ToastMessageMatcher", "" // <-- your error message);
            }
        }
        bufferedReader.close();
        process.waitFor();
        System.out.println(log.toString());
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }

This can likewise be directly in your derivatives too. 同样,这也可以直接存在于您的派生中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM