Check if a new Activity is started with Espresso
Well The solution is to check this way:
intended(hasComponent(OnboardingActivity::class.java.name))
In order to perform this check, you need to add the intents library (androidTestImplementation "androidx.test.espresso:espresso-intents:$intentsVersion")
and init it by calling:
Intents.init()
Is there any way to know if an activity has been started with Espresso?
Asserting something against a view that belongs to a certain activity is in my opinion a very elegant way of checking if that specific activity has been started. From the official docs:
At the same time, the framework prevents direct access to activities and views of the application because holding on to these objects and operating on them off the UI thread is a major source of test flakiness. Thus, you will not see methods like getView and getCurrentActivity in the Espresso API.
However, there is a way to accomplish what you need, like shown here. In my version, I also defined an assertion method like:
public void assertCurrentActivityIsInstanceOf(Class<? extends Activity> activityClass) {
Activity currentActivity = getActivityInstance();
checkNotNull(currentActivity);
checkNotNull(activityClass);
assertTrue(currentActivity.getClass().isAssignableFrom(activityClass));
}
which I used in the test methods.
For my own tests I didn't have any issues using it (Espresso 2.0 !), but it made it somewhat redundant since I would still check the views belonging to that activity. So it works, but I wouldn't recommend it.
Good luck!
EDIT:
There is also the possibility of checking if the intent was sent from your first activity to the second one (check this short tutorial), but that doesn't necessarily mean that the second activity displayed all of its views correctly. You should still check them being displayed, which brings you back to where you started.
Espresso - check which Activity is opened using intent on button press?
Whether it is possible to trace which the Activity opened after
pressing the button?
Check espresso-intents
library:
Configuration
Add to your app/build.gradle
these lines:
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
NOTICE:
espresso-intents
won't run withoutespresso-core
,runner
orrules
libs.
You may also need to change ActivityTestRule<>
to IntentsTestRule
as it is described here:
Example code (click on button to launch new activity)IntentsTestRule
Use IntentsTestRule instead of ActivityTestRule when using
Espresso-Intents. IntentsTestRule makes it easy to use
Espresso-Intents APIs in functional UI tests. This class is an
extension of ActivityTestRule, which initializes Espresso-Intents
before each test annotated with @Test and releases Espresso-Intents
after each test run. The activity will be terminated after each test
and this rule can be used in the same way as ActivityTestRule.From:
https://google.github.io/android-testing-support-library/docs/espresso/intents/
Here's a solution using espresso-intents
for similar problem:
An example test with intent stubbing:
@Test
public void testActivityResultIsHandledProperly() {
// Build a result to return when a particular activity is launched.
Intent resultData = new Intent();
String phoneNumber = "123-345-6789";
resultData.putExtra("phone", phoneNumber);
ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);
// Set up result stubbing when an intent sent to "contacts" is seen.
intending(toPackage("com.android.contacts")).respondWith(result));
// User action that results in "contacts" activity being launched.
// Launching activity expects phoneNumber to be returned and displays it on the screen.
user.clickOnView(system.getView(R.id.pickButton));
// Assert that data we set up above is shown.
assertTrue(user.waitForText(phoneNumber));
}
From:
https://developer.android.com/reference/android/support/test/espresso/intent/Intents.html
Additional resources:
[Android Developers] Espresso Intents Reference
[Github || Google Samples] Basic sample for Espresso Intents
[Github] Android Espresso Intent Sample
Testing for Android Intents using Espresso
[Gist] Example of how to use espresso-intents in Android tests - source code for link above
Android - Espresso How to test an view after clicked and moved to another Activity
I would use espresso intents
This way you capture that your app is going to open the other activity and respond with a result. Then, as the activity you are has not changed you can check the drawable there.
The code in your case should be something like:
@Test
fun checkClickImageView1() {
val result = Instrumentation.ActivityResult(Activity.RESULT_OK, null)
// Set up result stubbing when an intent sent to "OtherAcitivy2" is seen.
intending(hasComponent(OtherAcitivy2::class.java.name)).respondWith(result)
onView(withId(R.id.iv_button1)).perform(click())
onView(withId(R.id.iv_button1)).check(withDrawableMatcher(R.drawable.image1))
}
don't forget to add the espresso-intents
dependencies in your build.gradle
file.
Get Current Activity in Espresso android
In Espresso, you can use ActivityLifecycleMonitorRegistry
but it is not officially supported, so it may not work in future versions.
Here is how it works:
Activity getCurrentActivity() throws Throwable {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
runTestOnUiThread(new Runnable() {
@Override
public void run() {
java.util.Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = Iterables.getOnlyElement(activities);
}});
return activity[0];
}
Related Topics
Cannot Find Symbol Databindingcomponent on Android Studio 3.2 Canary 16 Kotlin Project
Add Elevation/Shadow on Toolbar for Pre-Lollipop Devices
How to Automatically Uninstall Android App from Device Before Installing a New Version
Equivalent of Clean & Build in Android Studio
How to Remove a Contact Programmatically in Android
How to Save the Image to Sd Card on Button Click Android
Change Text Color of Selected Item in Spinner
Can't Apply System Screen Brightness Programmatically in Android
Adt Doesn't Create Default Hello World But Command Line Does
How to Use the Proguard in Android Studio
How to Create a Number Picker Dialog
Android Tabwidget Detect Click on Current Tab
How to Change Background Color Popup Menu Android
How to Set Spinner Default Value to Null
Android Copy/Paste from Clipboard Manager
Execute Task Before Android Gradle Build