android-espresso


Android, Espresso. How check is soft keyboard is under view?


I have screen with EditText and button (under EditText). The requirement is when show soft keyboard it's must be under button. Is it possible to write Espresso unit test (or anoter test) that check this?
The android keyboard is part of the system and not your app, so espresso won't be enough here.
I created the following layout in my test activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.masta.testespressoapplication.MainActivity">
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="TEST" />
</RelativeLayout>
If you want to just use espresso the dirty solution would be:
#Test
public void checkButtonVisibilty2() throws Exception {
onView(withId(R.id.edittext)).perform(click());
try {
onView(withId(R.id.button)).perform(click());
throw new RuntimeException("Button was there! Test failed!");
} catch (PerformException e) {
}
}
This test would try to click on the button, which throws a PerformException since it would actually click on the Softkeyboard - which is not allowed.
But i would not recommend this way, it is quite a misuse of the espresso framework.
A solution a bit better imho would be using android UI automator:
#Test
public void checkButtonVisibilty() throws Exception {
onView(allOf(withId(R.id.edittext), isDisplayed())).perform(click());
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject button = mDevice.findObject(new UiSelector().resourceId("com.example.masta.testespressoapplication:id/button"));
if (button.exists()) {
throw new RuntimeException("Button is visible! Test failed!");
}
}
This uses android UI Automator to try fetch the button UI element and check if it exists in the current screen. (replace the package and id in the "resourceId" call with the ones for your case)
For android UI automator u need this additional gradle imports:
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile 'com.android.support:support-annotations:25.2.0'
A general thought: this kind of tests seems very error prone since you do not have real control over the soft keyboard and how it looks like, so i would use that with caution.

Related Links

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
Espresso - Using google espresso how to send email from Gmail app
Unable to perform PressBack in Espresso Test
espresso : Idling resource for many situations to wait

Categories

HOME
office365
visual-studio
ionic2
plesk
automapper
typelite
verification
enums
haproxy
stacktrace.js
embedded-resource
google-sheets-query
flurry
mosquitto
jint
native-base
squid
orange-api
robolectric
nose
http-authentication
wsf
adminlte
coreldraw
sharedpreferences
conda
pymongo
acl
device
ava
locks
es6-modules
location-services
reporting
epplus
grails-2.5
nppexec
wdk
textmate2
macports
python-sounddevice
classnotfoundexception
contactless-smartcard
julius-speech
audio-converter
daz3d
common-table-expression
viewmodel
aws-kinesis-firehose
roracle
ol3-google-maps
sequence-diagram
htmlspecialchars
popen
http-basic-authentication
wikimapia
glimpse
dblink
swiperefreshlayout
cognos-tm1
oid
mifos
elastic4s
jsonix
data-management
decoder
livecycle
callouts
xcopy
preferenceactivity
ioexception
webgrind
cortana
gdl
android-mapview
mousehover
astropy
fanotify
bjam
espresso
artisan
ocmockito
quercus
tidyr
directorysearcher
juttle
datagridcomboboxcolumn
zend-mail
dmake
pre
eclim
colon
discovery
apache-shindig
tnt4j
quickgraph
hints
microformats
reentrancy
office-app
onconfigurationchanged
clrprofiler
netstream
differentiation
snackjs
jquery-1.4
active-record-query
mygeneration
cassini
cleartype
ppc
external-sorting

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