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

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

Hi,

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?

try:
((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 ?

@Pankaj7

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(“http://127.0.0.1:4723/wd/hub”),dr);
WebElement title=driver.findElement(By.xpath("//android.widget.TextView[@text=‘Account (offline)’]"));
driver.closeApp();
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}]
Session ID: XXXX-XXXX-XXXX-XXXX-XXXX
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("http://127.0.0.1:4723/wd/hub"),dr);
driver.closeApp();

// 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();
driver.terminateApp(packageName);
driver.activateApp(packageName);