How to close Android app using UiAutomator?

How to close a specialized Android application using the UiAutomator API? For example, when you manually click the "Submit" button and swipe the application you want to close on the screen.

+10
source share
5 answers

The best option would be to use getUiDevice.pressRecentApps, this will download the latest applications for you, and then take a screenshot using the uiautomator viewer, you have the xml view of the loaded screen. Then you can use this xml to select the object you want to navigate using

UiObject app = new UIObject(new UiSelector().resourceId("The id of the app"); app.swipeLeft(100); 

or right

This may close your application. Xml will depend on what style is used for Android and the device.

+3
source

The best way (not device, OS version, user interface or orientation):

 Runtime.getRuntime().exec(new String[] {"am", "force-stop", "pkg.name.of.your.app"}); 

Tested and working on Nexus 5X with Android 6.0

+6
source

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.

+1
source

When it will be only one application that will be in the list of recent applications, it worked for me.

 if(mDevice.pressRecentApps()) { Thread.sleep(1000); int startX = 300; int startY =835; int endX = 1000; int endY = 835; // co-ordinates refer to x-axis from left of screen to right. int steps = 8;// speed at which the app closes mDevice.swipe(startX,startY,endX,endY,steps); } 
0
source

Here's how I kill all Android apps at once using uiautomator:

 public static void killApps() { UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); try { device.pressHome(); device.pressRecentApps(); // Clear all isn't always visible unless you scroll all apps down int height = device.getDisplayHeight(); int width = device.getDisplayWidth(); device.swipe(width/2,height/2, width/2, height, 50); UiObject clear = device.findObject(new UiSelector() .resourceId("com.android.systemui:id/button") .text("CLEAR ALL") ); if (clear.exists()) { clear.click(); } } catch (RemoteException e) { e.printStackTrace(); } catch (UiObjectNotFoundException e) { e.printStackTrace(); } } 
0
source

Source: https://habr.com/ru/post/976142/


All Articles