android-espresso


Espresso: How do i assert if a particular activity is launched when i click on a item using espresso


How do I assert an activity in Espresso after I click on a view item?
onView(withId(com.example.android.notepad.R.id.XYZ)).perform(click());
You should simulate the process of clicking a button and then test if the activity at the top of the stack is the one you're looking for
#RunWith(AndroidJUnit4.class)
public class YourTestClass{
#Test
public void testButton() {
Espresso.onView(ViewMatchers.withId(R.id.yourButtonId)).perform(ViewActions.click());
Assert.assertEquals(getActivityInstance().getClass(), YourActivityThatShouldStart.class);
}
private Activity getActivityInstance() {
final Activity[] currentActivity = {null};
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity[0] = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity[0];
}
}
On the testButton function, there are two lines, the first one to click on your button, the second one is to check the resulting activity
Espresso works on the main thread so your fine
Inside your tests you probably have this:
#Rule
public ActivityTestRule<MyActivity> testRule = new ActivityTestRule<>(MyActivity.class);
Then you can call:
testRule.getActivity()
to obtain instance of Activity.
the just call
testRule.getActivity() instanceof MyActivity

Related Links

How to test View in MVP+Dagger 2 in Android
Android, Espresso, ListView: How scroll to item and check this item?
Android, multiDexEnabled = true --> not start Espresso tests (NoClassDefFoundError)
Android, Espresso. How check is soft keyboard is under view?
How to validate whether opened correct activity - Espresso
Espresso Test Failed: Wanted to match 1 intents. Actually matched 0 intents
How test onData() column value is not null?
Android, Espresso: Is one view is under another view?
Espresso: How fast show new activity?
Android, Espresso: Show/Hide view depends of item content
Android. Unit test. Check is text is show in correct language
Cucumber Espresso and Android : How to use view matchers to find the particular view using resource id?
how to re-run failed tests using AndroidJunit4?
Espresso: typeText - not work in cyrilic
Espresso test - not recognize ad
Espresso: How do i assert if a particular activity is launched when i click on a item using espresso

Categories

HOME
php
android-studio
gaming
mpdf
add-on
loopbackjs
vsts-build
google-play-services
repair
wagtail
telegraf
bellman-ford
stack-overflow
yeoman-generator-angular
chromecast
vimeo
bootstrap-switch
android-externalstorage
jodatime
fresco
phonegap-build
cpu-usage
yii2-basic-app
stackexchange.redis
acl
web-config-transform
android-cardview
marquee
spring-annotations
javax.persistence
fusion
basic-authentication
unmarshalling
objectmapper
steam-web-api
opnet
mongodb-3.4
xcrun
cloudera-manager
jade4j
nanogallery
gitolite
sbjson
praat
export-to-pdf
vungle-ads
minikube
contactless-smartcard
expandablelistview
morphline
common-table-expression
return-type
comm
1010
gawk
gapi
ternary-operator
portal
metabase
google-identity-toolkit
e
livereload
dmarc
pyaudio
oci
static-methods
android-syncadapter
accessibilityservice
gdl
json-spirit
deeplink
godeps
miniconda
apache-commons-digester
ambiguity
blockquote
search-form
quercus
rfc5545
mathematical-expressions
baseadapter
adhoc-polymorphism
password-recovery
multivariate-testing
igor
windows-messages
code-complexity
xulrunner
wso2bam
pre
armv6
visual-c++-2005
saga
vlab
infinite
oembed
jsplitpane
android-dialog
kaazing
gamesalad
dropshadow
glx
qtextbrowser
botnet
database-deadlocks
outlook-2007-addin
android-input-method
resharper-5.0
getimagesize
html-components
efs
stretchblt

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App