am testing two Apps A and B . I need to go A keep it open and switch to APP B do some button clicks(while A is still running on background) and come back again to app A.
By the time I switch to app B using
// finish some tasks on B // driver.startActivity(new Activity(“com.example.APP_A”, “.MainActivity”)); The app A is dead. Whatever I expected to happen didn’t happen because its already got killed(I don’t know why)
So I tried ((AndroidDriver) driver).runAppInBackground(Duration.ofSeconds(80)); The above line before I make switch to app B . This works when I stop there and perform manual action in B an come back to A again.
But in automation since the above runAppInBackground method is synchronous …I can’t execute any actions I wanted to do in app B while A is running in background.
Either I wanted that runAppInBackground to be asynchronous method or some way I need to keep my APP A alive during switching to app B ? Any suggestions please ?
Hey Aleksei, I really appreciate your response.
I initially tried with (-1) but it did not work before I moved to (80). Anyways I retried with (-1) now but still it’s the same.
But I was badly looking for these ‘adb commands’ to take control and implement on my own. Can you please shed some light on that and an example please? I am using like this , but not sure what my command should like exact and also how would pass the adb path in the string array ?
String[] aCommand = new String[] { "adb", "shell", "am", "start","-d", "com.example.APP_B.MainActivity" };
try {
Process process = new ProcessBuilder(aCommand).start();
process.waitFor(10, TimeUnit.SECONDS);
System.out.println("Starting app B successfully!", hoping app A would now be running in background);
} catch (Exception e) {
e.printStackTrace();
}
}```
good to know , but when you wanted to come back to the old app again ,does the old app was not still not killed and retain’d its session data?
fyi ., I am able to switch to the different app , but the problem is my old app gets killed and not active.
Update from my side:
Before implementing in Java , I tried running the adb commands manually .
Opened the emulator , and opened the App A.
Went to the terminal , and got into adb path (/Users/XXXXXX/Library/Android/sdk/platform-tools)
run adb shell am start -n com.example.App_B/com.example.App_B.MainActivity
again go back or switch to app A(unresponsive)
Actual:
It switched to the new app B. But looks my old app A killed.
Expected
My old app needs to retain its session and should be running in background.
So its still the same behavior as ((AndroidDriver) driver).runAppInBackground(Duration.ofSeconds(-1));
As much as i wanted to know the reason on why these not working I am also looking to replicate /simulate the synchronous method runAppInBackGround() through abd command.
I tried adb shell set com.staples.APP_A RUN_IN_BACKGROUND but didnt work
Kindly help with adb commands for this ?
hey Aleksei,
The adb command is working when from terminal.
But the same command is not doing its job when run programatically from Java and throws no error too.
Is this a way adb command is usually run : String[] aCommand = new String[] { adbPath, "-s emulator-5554 shell am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "LOPE" " };
try {
Process process = new ProcessBuilder(aCommand).start();
process.waitFor(10, TimeUnit.SECONDS);
Process process2 = Runtime.getRuntime().exec(aCommand);
process2.waitFor(10, TimeUnit.SECONDS);
System.out.println("Broadcast event is done.");
} catch (Exception e) {
e.printStackTrace();
}```
Yes the full path would look like Users/xxxx/Library/Android/sdk/platform-tools/adb ? I have it on my code.
By the way , i am running locally on the same machine
Thanks Aleksei &Wreed both of you and One of my colleague Philip,Han who helped me set the correct PATH. FYI , if anyone looking for solution here set the path right by figuring out the correct RC and pass them to Processbuilder along with the PATH params in environment and the issue is resolved.