Getting app back to life from background

Hi guys!
I’ve faced quite interesting issue.
I need to set quite long sleep during the test like over 1 hour long or get app to background.
The problem is whenever phone is locked (or simply screen get black) it is seems that there is no possibility to:
Unlock/Active the phone and run again (from last acitivity) app to foreground.

I have tried with Thread.sleep (yes I know it is not the best solution) and with moving app to background.

Result is always the same:
Moving app to background result:

driver.runAppInBackground(Duration.ofSeconds(x));

[debug] [ADB] Found package: ‘debug’ and fully qualified activity name : ‘com.app.activity’
[debug] [MJSONWP] Responding to client with driver.background() result: null

Sleep try:

Thread.sleep(x);
button.click();

[UiAutomator] UiAutomator exited unexpectedly with code 0, signal null
[debug] [UiAutomator] Moving to state ‘stopped’
[Logcat] Logcat terminated with code 0, signal null
[debug] [AndroidDriver] Shutting down Android driver
[Appium] Closing session, cause was ‘UiAUtomator shut down unexpectedly’
[Appium] Removing session ‘id’ from our master session list

Have You ever faced similar problem???

Hi :slight_smile:

First you should use UIA2 instead of UIA (if you are using Android 5.0+)

To place in background, I would use:

driver.runAppInBackground(Duration.ofSeconds(-1));

After those 3600 seconds of inactivity I would run command:

driver.pressKeyCode(AndroidKeyCode.KEYCODE_WAKEUP);

to wake up the device in case it went to sleep. Then to activate your app again run:

driver.activateApp(ANDROID_PACKAGE);

Try this, and let me know if anything failed.

1 Like

The problem is that this API is good in theory , practically , 50% of the time it doesn’t really work , the app / device is being stuck from time to time

Hi @Telmo_Cardoso thank You for your tips! It seems that using UIA2 instead of UIA helped. But right now after ~25 minutes in background I got

Blockquote
org.openqa.selenium.WebDriverException: org.apache.http.NoHttpResponseException: 0.0.0.0:4723 failed to respond
Build info: version: ‘3.9.1’, revision: ‘63f7’, time: ‘2018-02-07T22:25:02.294Z’
System info: host: ‘.local’, ip: ‘en0’, os.name: ‘’, os.arch: ‘x86_64’, os.version: ‘10.13.3’, java.version: ‘1.8.0_131’
Driver info: driver.version: AppiumDriver

Ive tried with
capabilities.setCapability(“newCommandTimeout”, 0);

Hi,

not sure what newCommandTimeout with 0 does :slight_smile: But just use a value above the one you plan to be without sending commands to Appium server.

I dont know where that NoHttpResponseException come from… but if you sure that’s no problem with cable connection, then you can try instead of placing tests in idle for 1 hour, just send a driver.getPageSource every 5 minutes or so, to keep connection “alive”.

I never had any problem when app is on background, although I also never tested so much time.

With 0 it is my mistake. It should be very long duration(in my case).
NoHttpResponseException came always after ~25 minutes of keeping app in background.
I will notcie You if I find out some solution :wink:
Thank You!