Based on the solution from @ user597159, I got the following to close all applications in the Pixel 2 test lab for Firebase (that is, the device type is "walleye"):
private void killAllApps() throws Exception { boolean keepSwiping = true; int maxSwipeAttempts = 10; uiDevice.pressRecentApps(); for (int swipeAttempt=0; swipeAttempt<maxSwipeAttempts && keepSwiping; swipeAttempt++) { int height = uiDevice.getDisplayHeight(); int width = uiDevice.getDisplayWidth(); uiDevice.swipe(width / 2, height / 2, width, height / 2, 50); UiObject clearAll1 = uiDevice.findObject(new UiSelector().text("Clear all")); UiObject clearAll2 = uiDevice.findObject(new UiSelector().textStartsWith("Clear all")); UiObject clearAll3 = uiDevice.findObject(new UiSelector().textContains("Clear all")); UiObject clear = clearAll1.exists() ? clearAll1 : (clearAll2.exists() ? clearAll2 : clearAll3); if (clear.exists()) { Logger.debug(TAG, "Attempting to close app by killAllApps and found clear=all button on swipeAttempt=" + swipeAttempt); clear.click(); keepSwiping = false; } else { Logger.debug(TAG, "Attempting to close app by killAllApps but have to keep swiping swipeAttempt=" + swipeAttempt); keepSwiping = true; } } }
Note that on pixel 2 it says "Clear All," not "Clear All."
I could not get some other solutions to work. I got a UiObjectNotFoundException for the following:
app = uiDevice.findObject(new UiSelector().textContains("SDK Test App"));
And also for:
app = uiDevice.findObject(new UiSelector().className(com.locuslabs.android.sdk.SdkTestApplication.class));
In other words, app.exists() returned false for those approaches that tried to scroll the application to close on Pixel 2.
source share