I 've got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears.
Thats it until she reaches the end where I start a new Activity and display a score based on correct answers.
How should I start/test ( with Espresso I guess ) that activity without having to enter manually every time all the answers and click the button after each answer until I reach the last one?
What I need is to pass some mock data to a variable and make an Intent but I dont know how and cant find anything related with this in Espresso
You can launch your next activity with a custom intent like this:
@RunWith(AndroidJUnit4.class)
public class NextActivityTest {
@Rule
public ActivityTestRule<NextActivity> activityRule
= new ActivityTestRule<>(
NextActivity.class,
true, // initialTouchMode
false); // launchActivity. False to customize the intent
@Test
public void intent() {
Intent intent = new Intent();
intent.putExtra("your_key", "your_value");
activityRule.launchActivity(intent);
// Continue with your test
}
}
Full example: https://github.com/chiuki/android-test-demo
Blog post: http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html
First, see this question : Android Monkey Runner
Then you can see these guides :Monkey Runner
It makesyou usePython to test your android activity outside of your source. So, you can trigger things and get to specific activitiesl like this :
#! /usr/bin/env monkeyrunner
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from random import randint
print "get device"
device = MonkeyRunner.waitForConnection()
package = 'my.packaget'
activity = 'my.package.activity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)
#use commands like device.touch and device.drag to simulate a navigation and open my activity
#with your activity opened start your monkey test
print "start monkey test"
for i in range(1, 1000):
#here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
device.touch(randint(0, 1000), randint(0, 800), 'DOWN_AND_UP')
print "end monkey test"
save it and then run : monkeyrunner test.py
You can use the intent to launch dialer activity using below code.
@Rule
public IntentsTestRule<DialerActivity> mActivityRule = new IntentsTestRule<>(
DialerActivity.class);
private static final String PHONE_NUMBER = "1234567890";
private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + PHONE_NUMBER);
private static String PACKAGE_ANDROID_DIALER = "com.android.phone";
static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Starting with Android Lollipop the dialer package has changed.
PACKAGE_ANDROID_DIALER = "com.android.server.telecom";
}
}
@Test public void testDialerIntent()throws Exception
{
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
onView(withId(R.id.edit_text_caller_number)).perform(typeText(PHONE_NUMBER));
onView(withId(R.id.button_call_number)).perform(click());
intended(allOf(
hasAction(Intent.ACTION_CALL),
hasData(INTENT_DATA_PHONE_NUMBER),
toPackage(PACKAGE_ANDROID_DIALER)));
}
}
For more detailed description refer my blog post -
http://qaautomated.blogspot.in/2016/02/how-to-test-dialer-activity-with.html