EDIT: Updated description and error message and added some images. Still have this problem.
I have a strange error that occur many times when running espresso tests. Af
To avoid this, let your device stay awake. There is an option in the developer options of the device.
You could use com.android.ddmlib and a gradle task to change the device settings via adb for every build run and every device you are testing on.
import com.android.ddmlib.AndroidDebugBridge
import com.android.ddmlib.CollectingOutputReceiver
import com.android.ddmlib.IDevice
import com.android.ddmlib.NullOutputReceiver
task stayAwake {
description = "Activate the Stay Awake settings in the developer options."
group = "Device Setup"
AndroidDebugBridge.initIfNeeded(false)
def bridge = AndroidDebugBridge.createBridge(android.adbExecutable.path, false)
doLast {
bridge.devices.each {
it.executeShellCommand("settings put global stay_on_while_plugged_in 3", NullOutputReceiver.receiver)
println "Device ${it} will stay awake."
}
}
}
In addition you can activate the screen with another gradle task. (precondition is that no PIN or unlock pattern is set)
IDevice.metaClass.inputKeyEventByShell {
delegate.executeShellCommand("input keyevent ${it}", NullOutputReceiver.receiver)
}
IDevice.metaClass.inputSwipeByShell {
delegate.executeShellCommand("input swipe ${it}", NullOutputReceiver.receiver)
}
task unlockScreen {
description = "Activate screen and unlock device."
group = "Device Setup"
AndroidDebugBridge.initIfNeeded(false)
def bridge = AndroidDebugBridge.createBridge(android.adbExecutable.path, false)
doLast {
bridge.devices.each {
def receiver = CollectingOutputReceiver.newInstance()
it.executeShellCommand("dumpsys power | grep \"mHolding\"", receiver)
def displaySuspendFalse = receiver.getOutput().find("mHoldingDisplaySuspendBlocker=false")
def wakelockSuspendFalse = receiver.getOutput().find("mHoldingWakeLockSuspendBlocker")
if (displaySuspendFalse || wakelockSuspendFalse) {
it.inputKeyEventByShell('26') //power keyevent
println "Screen of device $it activated & unlocked."
}
it.inputSwipeByShell('100 500 100 1450 100') //swipe action
}
}
}
Let these tasks run before your gradle task responsible for the UI tests.
This way no additional code in your tests is required to activate the devices and keeping them alive.
Mainly the error is because if your connection is not in an active mode, it shouldn't be in sleep mode either it has to be in the app where the test cases have been written. Assuming you already used the @Rule Activity launch rule.
You can wake up your device before every test using Uiautomator.
@Before
public void init(){
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Point[] coordinates = new Point[4];
coordinates[0] = new Point(248, 1520);
coordinates[1] = new Point(248, 929);
coordinates[2] = new Point(796, 1520);
coordinates[3] = new Point(796, 929);
try {
if (!uiDevice.isScreenOn()) {
uiDevice.wakeUp();
uiDevice.swipe(coordinates, 10);
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
One possible cause of this is the tests are running in parallel. Use --no-parallel
.
Example --> gradlew connectedLiveDebugAndroidTest --no-parallel
It this happens only when running all of them from the console or sequentially, but doesn't happen running them individually then chances are they are running on parallel o the same device. Two different Espresso tests running on the same device at the same time make them flaky and prone to fail.
In my case, the solution was to use the UI Automater
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.findObject(UiSelector().textContains("text")).click()
I wanted to do clicks on an activity hosted by google login SDK. I noticed the activity triggerred by SDK is still visible even when the process of the app under test is killed. So I used UI Automator which is inteded for cross-app functional UI testing
I had the same problem. This happen when the screen on device is off. Turn screen on should fix the problem.