Writing to Android UI Automator output console

社会主义新天地 提交于 2019-12-05 03:26:28

You can use Instrumentation.sendStatus(..) report information to the console.

sendStatus(..) takes a Bundle and a status code as arguments. It won't let you write a string directly to the console, but each key / value pair in the Bundle will be written out like this:

INSTRUMENTATION_STATUS: key1=value1
INSTRUMENTATION_STATUS: key2=value2
INSTRUMENTATION_STATUS_CODE: -1

Note: This will only work if you're using a recent version of UiAutomator (2.0+). The old version does not have access to Instrumentation, so if you're using shell-based UiAutomator it's time to upgrade!

The Instrumentation.sendStatus(..) can be used to write to the uiautomator console.

Quick example will be:

   Bundle bundle = new Bundle();
   bundle.putString("MyResult","10");
   getAutomationSupport().sendStatus(0, bundle);

Hope this is what your looking for!

If running test from adb, the preferable way is printing to logcat:

import android.util.Log;

Log.d("My tag", "My log message");

On the other hand, last version of UIAutomator is used in test class implementing InstrumentationTestCase. This class is far ancestor of junit.framework.Assert (http://developer.android.com/reference/junit/framework/Assert.html). I assume you will find something useful from its methods. May be format method is what you are searching for.

If you want to use Java's print statement, you should import:

 import static java.lang.System.out;

After you do the import, then you can use:

out.println("hello world");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!