Search code examples
javaappiumappium-android

How not close application after driver.quit()


I am using Appium to install an application. After installation the application should work in the background. Another test should use Chrome to check Internet connectivity while the application is working in the background. The issue is that driver.quit() or even initiating a new instance of a AndroidDriver kills the application and it does not work in the background any more. Is there a way to test Chrome while the installed application is still working in the background?

public class test {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {

        AndroidDriver<MobileElement> driver;
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");

        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Xiaomi A2 Lite");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
        capabilities.setCapability(MobileCapabilityType.APPLICATION_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES, "false");
        capabilities.setCapability(MobileCapabilityType.NO_RESET, "true");
        capabilities.setCapability(MobileCapabilityType.FULL_RESET, "false");
        capabilities.setCapability("appPackage", "com.waze");
        capabilities.setCapability("appActivity", "com.waze.FreeMapAppActivity");

        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        Thread.sleep(5000);


        DesiredCapabilities capabilities2 = new DesiredCapabilities();
        capabilities2.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
        capabilities2.setCapability(MobileCapabilityType.DEVICE_NAME, "Xiaomi A2 Lite");
        capabilities2.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
        capabilities2.setCapability(MobileCapabilityType.APPLICATION_NAME, "Android");
        AndroidDriver<MobileElement> driver2;

        driver2 = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities2);
        driver2.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver2.get("http://google.com");

        MobileElement googleLogo=(MobileElement) driver2.findElement(MobileBy.id("hplogo"));
        Assert.assertEquals(googleLogo.isDisplayed(), true);

    }

}


Solution

  • Found a way to keep application in the background while opening another application by using

    Activity activity = new Activity(appPackage, appActivity);
    driver.startActivity(activity);
    

    However this solution do not work if driver was initiated while app capability is set -for installing apk file

    capabilities.setCapability(MobileCapabilityType.APP, apkPath);