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

I want to scroll on recycler view till the element appears on screen, it may make network calls after each 10 elements.
Mock server requests Android Espresso UI Testing
Espresso mockWebServer response does not intercept http calls
Android Instrumentation pull test results/files from device
Espresso hangs the UI and the System kills the application
Espresso test with phone asleep
Unable to run espresso test
How to force Espresso wait for Eventbus posts?
Espresso Web + Chrome cust tab
Espresso 2 - How to Test Multiple Activities?
Android Espresso test to check syncness between ViewPager's fragments and TabViews
Is there a way to automate API calls using Espresso?
Android Espresso - Single list with same items (AmbiguousViewMatcherException)
Do Matchers extending hamcrest.BaseMatcher have to be consistent
testing http connection on google cloud test lab devices
Espresso Test Recorder has detached from the device VM

Categories

HOME
google-chrome
android-espresso
powerbi
schema.org
exchange-server
add-on
theano
apk
dronekit-python
greasemonkey
iptables
angular-meteor
graphql-js
roku
lc3
graphdb
createjs
sql-injection
mixpanel
http-authentication
significance
angular2-highcharts
azure-machine-learning
yii2-basic-app
tableview
liferay-6.2
fluent-migrator
mod-wsgi
jackrabbit-oak
acrobat
region
om-next
db2-luw
textmate2
nsuserdefaults
mv
widestring
spring-restcontroller
distributed-caching
vungle-ads
permission-denied
apache-toree
nomad
absolute
icecast
renderman
android-webservice
controllers
ipywidgets
java-2d
ndk-build
wininet
maven-release-plugin
right-to-left
yii2-model
wicked-pdf
python-cffi
html.actionlink
asp.net-web-api-helppages
blank-line
sqlbase
hmisc
compositetype
google-gdk
explain
fuseesb
phonegap-facebook-plugin
jolie
relationships
ecos
commenting
ivalueconverter
pre
django-sites
smips
visual-c++-2005
unrealscript
ax
xs
blobstorage
html-frames
jbox2d
oembed
significant-digits
cyrillic
email-verification
system-requirements
semantic-merge
multiple-dispatch
zotonic
hardcode
dropshadow
quartz-core
background-repeat
kohana-auth
printqueue
ninject-interception
ccnet-config
rakudo
downloadfile

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App