driver.quit() 后如何不关闭应用程序

How not close application after driver.quit()

我正在使用 Appium 安装应用程序。安装后,应用程序应在后台运行。另一个测试应该使用 Chrome 在应用程序在后台运行时检查 Internet 连接。 问题是 driver.quit() 甚至启动 AndroidDriver 的新实例都会终止应用程序,并且它不再在后台运行。 有没有办法在已安装的应用程序仍在后台运行时测试 Chrome?

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);

    }

}

您可以想出一个示例 class 实现 Callable 接口:

import io.appium.java_client.AppiumDriver;

import java.time.Duration;
import java.util.concurrent.Callable;

public class BackgroundThread implements Callable {

    AppiumDriver driver;
    Duration duration;

    public BackgroundThread(AppiumDriver driver, Duration duration) {
        this.driver = driver;
        this.duration = duration;
    }

    @Override
    public Object call() {
        driver.runAppInBackground(duration);
        return "Slept for " + duration.toString();
    }
}

当您需要将应用程序发送到后台并在它存在时执行其他操作时 - 使用 ExecutorService:

调用它
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Object> result = executorService.submit(new BackgroundThread(driver, Duration.ofSeconds(60)));

您可能还会发现 DeviceAction 命令很有用,例如,您可以模仿 "Home" 按钮单击,这会将应用程序发送到后台并立即 return 上下文。

找到了一种在打开另一个应用程序时将应用程序保持在后台的方法

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

但是,如果在设置应用程序功能时启动驱动程序,则此解决方案不起作用 - 用于安装 apk 文件

capabilities.setCapability(MobileCapabilityType.APP, apkPath);