How to detect Toast messages using Robotium?

1,846 views
Skip to first unread message

Julian Harty

unread,
Dec 30, 2010, 1:15:33 PM12/30/10
to Robotium Developers
Hi Renas,
I've been experimenting with Robotium to see what capabilities it
offers. I noticed you mention explicitly that it supports Toasts
however I've not managed to work out how to convince it to detect a
Toast message, etc. Please, can you help me understand what I'd need
to do to in order to detect Toast messages, capture the contents of
the Toast message, etc? Thank you.

Here's what I've done so far:
1. Read the source code for robotium (after cloning it to my machine
using git).
2. Searched the examples including https://github.com/jayway/robotium-samples
, Stack Overflow, etc but didn't find anything that seemed to interact
with a Toast message
3. Hacked the example from http://wiebe-elsinga.com/blog/?p=300 as
follows:

In the demo app, in Main.java edited display to create a simple Toast
message

protected void display(Editable text) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;

Toast.makeText(context, "Toast", duration).show();

// output.setText(text);
}

In the demo test, in MainTest.java in testDisplay() tried both:
assertTrue(this.solo.waitForText("Toast"));
assertTrue(this.solo.searchText("Toast"));

Note: I used version 1.7.1 of the prebuilt version of the robotium JAR
as the example test didn't seem to interact with the App being tested
when I used the current 2.0.2 version of the robotium JAR. I think
I've seen a couple of comments on this issue, I'll dig them out and
test with 2.0.2 soon.
Thanks

Julian

Renas Reda

unread,
Dec 30, 2010, 1:24:31 PM12/30/10
to Robotium Developers
Hi Julian,

The way that you are using searchText and waitForText is correct, they
do search toasts. In that particular version there was a bug when
searching toasts and that is why it does not work for you. I would
highly recommend you to use the latest version as it is much better in
every way compared to the 1.7.1 version. What kind of difficulties
have you encountered when you tried to use the 2.0.2 version instead
of 1.7.1?

/Renas

On 30 Dec, 19:15, Julian Harty <julianha...@gmail.com> wrote:
> Hi Renas,
> I've been experimenting with Robotium to see what capabilities it
> offers. I noticed you mention explicitly that it supports Toasts
> however I've not managed to work out how to convince it to detect a
> Toast message, etc. Please, can you help me understand what I'd need
> to do to in order to detect Toast messages, capture the contents of
> the Toast message, etc? Thank you.
>
> Here's what I've done so far:
> 1. Read the source code for robotium (after cloning it to my machine
> using git).
> 2. Searched the examples includinghttps://github.com/jayway/robotium-samples

Julian Harty

unread,
Dec 30, 2010, 1:53:51 PM12/30/10
to Robotium Developers
The Test doesn't type into the EditText field (here I'm referring to
the example from http://wiebe-elsinga.com/blog/?p=300 )

Julian Harty

unread,
Dec 30, 2010, 1:58:22 PM12/30/10
to Robotium Developers
Here's the stacktrace from the failing test

junit.framework.AssertionFailedError: EditText with index 2131034112
is not available!
at com.jayway.android.robotium.solo.Solo.enterText(Solo.java:1155)
at com.my.MainTest.testDisplay(MainTest.java:53)
at java.lang.reflect.Method.invokeNative(Native Method)
at
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:
204)
at
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:
194)
at
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:
186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:
520)
at android.app.Instrumentation
$InstrumentationThread.run(Instrumentation.java:1447)



On Dec 30, 6:24 pm, Renas Reda <renasr...@gmail.com> wrote:

Julian Harty

unread,
Dec 30, 2010, 2:01:20 PM12/30/10
to Robotium Developers
Sorry for lots of small posts, however here's yet another - the
failing ID matches the one generated in R.java for R.id.EditText01
(I've copied the entire test (with hacks) here)

package com.my;

import java.util.ArrayList;

import com.jayway.android.robotium.solo.Solo;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

/**
* @author W.Elsinga
*
*/
public class MainTest extends ActivityInstrumentationTestCase2<Main> {

private Solo solo;
private Activity activity;

/**
*
*/
public MainTest() {
super("com.my", Main.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

this.activity = this.getActivity();
this.solo = new Solo(getInstrumentation(), this.activity);
}

@Override
public void tearDown() throws Exception {
try {
this.solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
this.activity.finish();
super.tearDown();
}

/**
* @throws Exception Exception
*/
public void testDisplay() throws Exception {
String text = "Congratulations";

//Enter "Congratulations" inside the EditText field.
this.solo.enterText(R.id.EditText01, text);

//Click on the button named "Click".
this.solo.clickOnButton("Click");

//Check to see if the given text is displayed.
assertTrue(this.solo.waitForText("Toast"));
assertTrue(this.solo.searchText("Toast"));

// TextView outputField = (TextView)
this.activity.findViewById(R.id.TextView01);
// ArrayList<TextView> currentTextViews =
this.solo.getCurrentTextViews(outputField);
// assertFalse(currentTextViews.isEmpty());
//
// TextView output = currentTextViews.get(0);
// assertEquals(text, output.getText().toString());
}

}


On Dec 30, 6:24 pm, Renas Reda <renasr...@gmail.com> wrote:

Julian Harty

unread,
Dec 30, 2010, 2:07:02 PM12/30/10
to Robotium Developers
And from reading the source code in Solo.java, I'm guessing the change
in behaviour is related to the call to
RobotiumUtils.removeInvisibleViews();

Renas Reda

unread,
Dec 30, 2010, 2:15:56 PM12/30/10
to Robotium Developers
The problem is that you are using the resource id of the EditText.
With enterText(int index, string text) you basically just state which
edit text field that you want to enter text into. So in your case it
would probably be enterText(0, "test"); If you would have one more
edit text then that one would be enterText(1, "test"). If you want to
use the resource id instead you could do something like this: EditText
editText = (EditText) solo.getView(R.id.x) and then you could use that
in enterText(editText, "Test") as it is also possible to use enterText
with an edit text object.

/Renas

Julian Harty

unread,
Dec 30, 2010, 2:44:58 PM12/30/10
to Robotium Developers
Thanks for the speedy solution, which makes sense in terms of the
problem and the fixes you proposed. I've tried both the approaches you
suggest and both worked fine :)

I double-checked the example code from http://wiebe-elsinga.com/blog/?p=300
and it seems that for whatever reason the original example worked with
the older version of robotium (1.7.1 or 1.6.0 as he used in his
example code). From looking at the method enterText(int, String) in
1.7.1 perhaps there was a bug or other side-effect with the older
libraries?

Anyway, I'm pleased to see Robotium does detect the contents of Toast
messages. As a bonus, is it possible to specify that we explicitly
want to find the message in a Toast message? (From reading a post on
StackOverflow http://stackoverflow.com/questions/2405080/how-to-test-for-the-appearance-of-a-toast-message
) I'm guessing this would be hard to do.

Julian

Renas

unread,
Dec 30, 2010, 4:07:48 PM12/30/10
to robotium-...@googlegroups.com

No you can not specify that. I am glad that it is working well for you now.

/Renas

> --
> You received this message because you are subscribed to the Google Groups "Robotium Developers" group.
> To post to this group, send email to robotium-...@googlegroups.com.
> To unsubscribe from this group, send email to robotium-develo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/robotium-developers?hl=en.
>
Reply all
Reply to author
Forward
0 new messages