Appium session如何长时间保持存活

How to keep alive Appium session for long time

我需要在测试过程中等待 5 分钟,但 Appium 会话具有默认值 newCommandTimeout of 60s.,我在下一个命令中遇到异常,提示我的会话超时。

AndroidDriver appiumDriver = new AndroidDriver(new URL(getMcmUrl()), capabilities);
Thread.sleep(5*60*1000); // 5 minutes sleep time
appiumDriver.executeScript("...")

新命令超时:

How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session

如果超时为60s,您需要每分钟至少执行一次命令,以保持会话存活。

例如,这是睡眠 5 分钟的样子

for (int i = 0; i < 5; i++) {
    driver.getOrientation(); // execute some command to keep the session alive
    Thread.sleep(59*1000); // wake up before session expired
}

阅读本文了解更多信息
https://l18.me/how-to-keep-alive-appium-driver-da9227b2fa

您是否考虑过并拒绝覆盖 newCommandTimeout?这肯定会奏效,但也有缺点。

尝试使用此命令,

"cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");"

通过此命令,appium 服务器将在关闭前等待 100 秒的命令。您可以根据自己的喜好增加超时时间。

在您的 DesiredCapabilities 中添加 newCommandTimeout 功能。

DesiredCapabilities caps=new DesiredCapabilities();
//other desired caps
//add the following line
caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
//then define your driver here
AppiumDriver<MobileElement> driver= new AndroidDriver(new URL(getMcmUrl()), caps);    

newCommandTimeout means How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session.

300 sec = 5 minutes