gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:15:04 29 4
gpt4 key购买 nike

我正在使用 Appium 安装应用程序。安装后,应用程序应该在后台运行。另一项测试应使用 Chrome 在应用程序在后台运行时检查互联网连接。问题是 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);

}

}

最佳答案

您可以想出一个实现 Callable 的示例类接口(interface):

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命令很有用,例如您可以模仿“主页”按钮单击,这会将应用程序发送到后台并立即返回上下文。

关于java - driver.quit() 之后如何不关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309241/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com