In the process of testing automation, it is sometimes necessary to obtain a string of toast for inspection.
In Robotium, we know that we can get the textview of a toast through the Solo.getview ("message") method, and then get its string value, so how is it implemented internally?
Let's start by looking at how we typically call toast:
Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show ();
When you call the Maketext () method of a toast in your app, the system does the following things:
public static Toast Maketext (context context, charsequence text, int duration) { Toast result = new toast (context);
layoutinflater inflate = (layoutinflater) context.getsystemservice (context.layout_inflater_service); View v = inflate.inflate (com.android.internal.r.layout.transient_notification, null); TextView TV = (TextView) V.findviewbyid (com.android.internal.r.id.message); Tv.settext (text); Result.mnextview = V; result.mduration = Duration; return result; }
from the above, when calling Maketext, the system Initializes a TextView, and the ID of this TextView ID system is: com.android.internal.r.id.message
Knowing that the essence of a toast is a textview, and that its ID is com.android.internal.r.id.message, the string to get it is done.
See the implementation of the GetView () method in Robotium:
/** * Returns a {@code View} with a given ID. * * @param id The ID of the {@link view} to return * @param index the index of the {@link view}. {@code 0} If only one was available * @return a {@code view} with a given ID */public view GetView (String ID, int index) {Vi EW viewtoreturn = null; Context Targetcontext = Instrumentation.gettargetcontext (); String PackageName = Targetcontext.getpackagename (); int viewId = Targetcontext.getresources (). Getidentifier (ID, "id", packagename); if (viewId! = 0) {Viewtoreturn = GetView ( ViewId, index, TIMEOUT); }if (Viewtoreturn = = null) {int androidviewid = Targetcontext.getresources (). Getidentifier (ID, "id", "Android"); if ( Androidviewid! = 0) {Viewtoreturn = GetView (Androidviewid, index, TIMEOUT);}} if (Viewtoreturn! = null) {return viewtoreturn;} Return GetView (viewId, index); }
Robotium to find the control in a string ID, it encapsulates a method such as GetView (string ID, int index) to get the view by a string ID. In this method, by getidentifier the ID of the string form to the ID of the int, and then looking for the control based on the ID of the int type, we already know the toast ID from the above, so we can simply pass the Solo.getview (" Message ") to get the textview of the toast.
Of course, in order to get a better toast in the actual project, we can encapsulate it ourselves:
/** * Gets the string value of the toast * @return */public string gettoast (int timeout) {TextView toasttextview = null; String toasttext = ""; Long EndTime = Systemclock.uptimemillis () + Timeout;while (Systemclock.uptimemillis () < EndTime) {Toasttextview = (TextView) getView ("message", 0); if (null! = Toasttextview) {Toasttext = Toasttextview.gettext (). ToString (); break;} else {Sleeper.sleepmini ();}} return toasttext;}
Well, it's cold and it's a piece of water.
Android gets a toast string parsing