Want to restart Android application in a single Appium session without deleting/uninstalling it

Hello guys.

I want to test my application with different set of test data for login functionality. Once i open my app and give username and password and close the app and open the app again to test with another set of credentials.

I want to restart my application (without deleting it) by calling driver.closeApp() followed by Driver.LaunchApp().(I want all these things to be happen in single Appium session)

I am not able to open the app in the same appium session.

If I want to achieve both i.e. Uninstalling/Installing while creating new Appium session at start of the test and in between test when I will close and relaunch app it should not delete/uninstall my application,which desiredcapabilities should I set?

Thanks and Regards,
Sankar S.V.U

1 Like
try{driver.runAppInBackground(1);}catch (Exception e) {}
1 Like


Showing an error:
driver.closeApp(); - The method closeApp() is undefined for the type WebDriver

runAppInBackground(1) - The method runAppInBackground(int) is undefined for the type WebDriver

Please help

why your “driver” is WebDriver for android app?

((AndroidDriver) driver).closeApp();
try{((AndroidDriver)driver).runAppInBackground(1);}catch (Exception e) {}

It’s working.

Thank you!

Alternatively, you can use resetApp() method, which is going to clear user data and restart the app.

if we call "driver.closeApp(); it will close the app instance.
if we now call driver.runAppInBackground(1) how it will run the app in background and relaunch it when we already closed the application ?


with our app it is just look as start.

Please find below code snippet along with the error log.

DesiredCapabilities dr=new DesiredCapabilities();
dr.setCapability(“deviceName”, “Moto”);
dr.setCapability(“platformName”, “Android”);
dr.setCapability(“appPackage”, “com.google.android.youtube”);
dr.setCapability(“appActivity”, “com.google.android.youtube.HomeActivity”);
AppiumDriver driver = new AndroidDriver(new URL(“”),dr);
WebElement title=driver.findElement(By.xpath("//android.widget.TextView[@text=‘Account (offline)’]"));
try{driver.runAppInBackground(1);}catch (Exception e) {}
String str=title.getText();

Error Log : FAILED: TestClass
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 28 milliseconds
Build info: version: ‘3.4.0’, revision: ‘unknown’, time: ‘unknown’
System info: host: XXXXXXX’, ip: ‘XX.XX.XX.XXX’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_121’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.google.android.youtube, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=XXXXXX, platform=LINUX, appActivity=com.google.android.youtube.HomeActivity, desired={appPackage=com.google.android.youtube, appActivity=com.google.android.youtube.HomeActivity, platformName=Android, deviceName=Moto}, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

Please suggest, here the mentioned code is not working.

@Pankaj7 what line of code is problematic? i do not see in log…

also i see that code not quite in correct order. change to:

// start driver and close app
AppiumDriver driver = new AndroidDriver(new URL(""),dr);

// start app
try{driver.runAppInBackground(1);}catch (Exception e) {}

// find element and get it text
WebElement title=driver.findElement(By.xpath("//android.widget.TextView[@text='Account (offline)']"));
String str=title.getText();

App is not starting using code

try{driver.runAppInBackground(1);}catch (Exception e) {}

@chetan_thummar replace with:

String packageName = ((AndroidDriver) driver).getCurrentPackage();

Thank you @Aleksei It’s working perfectly with packageName

@Aleksei I am new in appium coding, and i have a scenario where i want to close the app and start again ( basically want to restart here), and i am trying to use your given code, in my test case, but getting compile time error - “The method runAppInBackground(Duration) in the type InteractsWithApps is not applicable for the arguments (int)”

And I don’t know where i have to use this package code( do i need to use it ??)

You should use duration of time now. See example

I think that should work, will try and revert you back …
Thanks for this helpful information

Thank you @Aleksei It’s working perfectly for me

@Aleksei I was using the below snippet and the driver is Android Driver

String packageName = driver.getCurrentPackage();

I got this error

[AndroidDriver] ‘com.android.launcher3’ is still running after 500ms timeout
An unknown server-side error occurred while processing the command. Original error: ‘com.android.launcher3’ is still running after 500ms timeout

Also used the below snippet and that also produced the error which is listed below

}catch (Exception e) {}


[AndroidDriver] Cannot activate ‘com.android.launcher3’. Original error: Error executing adbExec. Original error: ‘Command ‘C:\Users\xyz\AppData\Local\Android\Sdk\platform-tools\adb.exe -P 5037 -s emulator-5554 shell monkey -p com.android.launcher3 -c android.intent.category.LAUNCHER 1’ exited with code 252’; Stderr: ‘args: [-p, com.android.launcher3, -c, android.intent.category.LAUNCHER, 1] [AndroidDriver] arg: “-p” [AndroidDriver] arg: “com.android.launcher3” [AndroidDriver] arg: “-c” [AndroidDriver] arg: “android.intent.category.LAUNCHER” [AndroidDriver] arg: “1” [AndroidDriver] data=“com.android.launcher3” [AndroidDriver] data=“android.intent.category.LAUNCHER”’; Code: ‘252’

with latest appium 1.20+ use just:

driver.launchApp(); // actual behavior kill and start
1 Like

@Aleksei launchApp() is deprecated in Appium-Python-Client(v3.0.0). How can I achieve a restart in a single session in Appium-Python-Client(v3.0.0)?