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

Espresso: String in adapterView
Espresso: Verify if one or more listView items contains an item with a substring
Android Espresso performs longClick instead of click
how to make assert to wait for IdlingResource to assert
Espresso test is passed only first time. Then is failed constantly because of NoMatchingViewException
Espresso2.0 typeText(string) bug?
Running espresso tests without reinstalling application
Scroll the screen to bottom without scrollview and without ID and without firstChild android-espresso automation
DropDown value selection using espresso android with dynamic element id's
matches(not(isDisplayed())) fails with NoMatchingViewException
Android Espresso: How to import LongListMatchers in v2.0?
How to get count of items with same ids which are not in adapter view
Google Espresso: Do I need application code in my workspace along with test project?
How to use espresso for testing a 3rd party app
How to verify if text is selected
Clicking on a specific child of a ListView in Espresso

Categories

HOME
google-chrome
facebook-messenger
exchange-server
couchdb
softlayer
out-of-memory
consul
google-webmaster-tools
google-sheets-query
navigation
anchor
selection
stack-overflow
swarm
heuristics
match
phpstorm-2017.1
google-cloud-logging
vertica
exec-maven-plugin
varnish-vcl
eclipse-emf
raima
fusetools
uri
tortoisehg
cube
sfml
sales
location-services
quick-nimble
cloudera-manager
serversocket
subclassing
numerics
macports
swfupload
grails3.2.0
contactless-smartcard
spooler
dbcc
git-tfs
knockout-2.0
beeline
nusoap
cordova-ios
aurelia-fetch-client
asp.net-web-api-routing
mpmovieplayercontroller
ltrace
gawk
jsonix
dcount
bilinear-interpolation
fuzzer
conan
cdo.message
processor
textblock
persistent-object-store
diawi
textpattern
html.actionlink
mousehover
directx-9
gql
search-form
artisan
actionpack
kotlin-android-extensions
mathematical-expressions
nhibernate-criteria
juttle
string.format
image-rotation
xml-signature
inequality
ecos
coding-efficiency
saga
unrealscript
disjoint-union
quickgraph
postgres-xc
bcdedit
jbox2d
oembed
xmlwriter
ckeditor.net
lynx
multiple-dispatch
getopt-long
jdom
n-queens
dynamic-c
differentiation
will-paginate
kernel32
hungarian-notation
sscli
yahoo-maps
downcasting
rendering-engine
error-detection

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