How to terminate app after each test Appium UIAutomator2

I’ve been using older version of Appium and appium-java-client and recently I’ve been trying to upgrade to new versions. For closing the app after each android test I used mobileDriver.quit() but it’s not working in the new version. I’ve read the documentation and found a command mobileDriver.executeScript("mobile: terminateApp", ImmutableMap.of("appId", "app.id")) but it won’t work either. Has anyone been able to achieve this? Thanks in advance.

Have you tried terminateApp from here:

I checked this out but I still wasn’t able to implement these methods, NullPointerException. Found a workaround tho, I removed noReset option from UIAutomatorOptions and now when new session starts, the app restarts so I don’t need to close it via driver. Thanks for your help.

    @Step("Terminate app")
    public AppDriver terminateApp() {
        Logger.log();
        String appID = null;
        if (driver != null) {
            try {
                if (driver instanceof AndroidDriver) {
                    appID = (String) driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_PACKAGE);
                } else if (driver instanceof IOSDriver) {
                    appID = String.valueOf(driver.getCapabilities().getCapability(IOSMobileCapabilityType.BUNDLE_ID));
                } else
                    Assert.fail(createAssertionLog("unknown driver type"));
                if (appID != null)
                    ((InteractsWithApps) driver).terminateApp(appID);
            } catch (Exception e) {
                Logger.logError(e.getMessage());
            }
        }
        return this;
    }
1 Like

This works for me. Thanks Aleksei!