I am writing a small wrapper over Android UI Automator. Normally we can see the test case status in the console. Can I access it and add my own messages? I have tried System.out.println
. But it did not work.
Is there any way of doing this?
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");
来源:https://stackoverflow.com/questions/31453501/writing-to-android-ui-automator-output-console