7

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("...")
Meister96Fels
  • 508
  • 1
  • 8
  • 26
Elhay Avichzer
  • 836
  • 1
  • 10
  • 14
  • 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. – Elhay Avichzer Jan 23 '19 at 16:52

4 Answers4

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

Elhay Avichzer
  • 836
  • 1
  • 10
  • 14
5

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

Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
1

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

Mike Collins
  • 4,108
  • 1
  • 21
  • 28
  • 1
    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). – Elhay Avichzer Jan 23 '19 at 16:44
  • Yep, it's a last resort or quick debugging option. Your solution is much better. – Mike Collins Jan 23 '19 at 18:28
  • Or you could just have different capabilities for that one test. – Angusiasty Jan 24 '19 at 14:15
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.

Nauman Malik
  • 188
  • 8
  • 1
    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. – Bill Hileman Jan 25 '19 at 16:07
  • Hey, Thanks for the response. But I have used the above command with parameter as a string value and it is working for me. – Nauman Malik Jan 28 '19 at 04:31
  • Fair enough. Perhaps the inner-workings of the capabilities processor can handle numeric values passed as strings. – Bill Hileman Jan 28 '19 at 14:28