How to keep alive Appium session for long time
Asked Answered
B

4

8

I need to wait in the middle of my test for 5 minutes but the Appium session has default newCommandTimeout of 60s. and I get exception in the next command that my session is timeout.

AndroidDriver appiumDriver = new AndroidDriver(new URL(getMcmUrl()), capabilities);
Thread.sleep(5*60*1000); // 5 minutes sleep time
appiumDriver.executeScript("...")
Bellhop answered 22/1, 2019 at 12:9 Comment(1)
i rather to not change newCommandTimeout capability to five minutes, just be able to keep it alive for dynamic time, while i perform some background logic, for example.Bellhop
B
7

newCommandTimeout:

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

in case that the timeout is 60s, you need to execute any command at least once in a minute, to keep the session alive.

For example, this is how sleep for 5 minutes should look like

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
}

Read this article for more information
https://l18.me/how-to-keep-alive-appium-driver-da9227b2fa

Bellhop answered 22/1, 2019 at 12:9 Comment(0)
S
6

In your DesiredCapabilities add newCommandTimeout capabilities.

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

Stride answered 23/1, 2019 at 5:8 Comment(0)
M
1

Have you considered and dismissed overriding the newCommandTimeout? That will certainly work, but has downsides.

Munro answered 22/1, 2019 at 19:50 Comment(3)
This can work but it has some limits, use this solution only if: 1. you already know the amount of time you need. 2. it is always the same time. 3. You don’t care if you always have long idle timeout, before the driver expired. (even when you don’t need).Bellhop
Yep, it's a last resort or quick debugging option. Your solution is much better.Munro
Or you could just have different capabilities for that one test.Sanitary
T
1

try using this command,

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

By this command the appium server will wait for 100 seconds for a command before shutting down. You can increase the timeout for your preference.

Tyranny answered 23/1, 2019 at 4:15 Comment(3)
I won't downvote your answer because I don't think honest effort should be punished, but the parameter is a numeric value, not a string. See Suban's answer which agrees with what I use with my code.Chatelain
Hey, Thanks for the response. But I have used the above command with parameter as a string value and it is working for me.Tyranny
Fair enough. Perhaps the inner-workings of the capabilities processor can handle numeric values passed as strings.Chatelain

© 2022 - 2024 — McMap. All rights reserved.